SPRING Security เป็นโมดูลขนาดใหญ่และบทความนี้เกี่ยวข้องกับการตรวจสอบความถูกต้องของพารามิเตอร์ที่กำหนดเองเท่านั้น พารามิเตอร์การตรวจสอบเริ่มต้นของความปลอดภัยในสปริงเป็นเพียงชื่อผู้ใช้และรหัสผ่านซึ่งโดยทั่วไปไม่เพียงพอ เนื่องจากเวลาผ่านไปนานเกินไปบางคนลืมและบางคนอาจหายไป ตกลงไม่มีเรื่องไร้สาระ
การกำหนดค่าความปลอดภัยในฤดูใบไม้ผลิและสปริงใช้ Javaconfig และรุ่นคือ 4.2.5 และ 4.0.4 ในทางกลับกัน
แนวคิดโดยรวม: ปรับแต่งจุดเข้าใช้งานเพิ่มพารามิเตอร์ที่กำหนดเองเพื่อขยายการรับรองความถูกต้องและการรับรองความถูกต้องสำหรับการตรวจสอบ
ก่อนกำหนดจุดเข้า:
นำเข้า org.springframework.security.core.authenticationexception; นำเข้า org.springframework.security.web.authentication.loginurlauthenticationEntrypoint; นำเข้า javax.servlet.servletexception; javax.servlet.http.htttservletResponse; นำเข้า java.io.ioexception; คลาสสาธารณะ myauthenticationentrypoint ขยาย loginurlauthenticationentrypoint {สาธารณะ myauthenticationentrypoint (String loginformurl) } @Override โมฆะสาธารณะเริ่มต้น (คำขอ httpservletRequest, การตอบสนอง httpservletResponse, การรับรองความถูกต้องของการรับรองความเป็นจริง) โยน ioexception, servletexception {super.commence (คำขอ, การตอบสนอง, การรับรองความถูกต้อง); -ถัดไปคือโทเค็น ValidCode คือพารามิเตอร์รหัสการตรวจสอบ:
นำเข้า org.springframework.security.authentication.usernamepasswordauthenticationToken; คลาสสาธารณะ MyuserNamePasswordauthenticationToken ขยาย UserNamePasswordAuthenticationToken {สตริงส่วนตัว ValidCode; สาธารณะ myusernamepasswordauthenticationToken (String Principal, String Credentialss, String ValidCode) {super (principal, credentials); this.validCode = ValidCode; } สตริงสาธารณะ getValidCode () {return validCode; } โมฆะสาธารณะ setValidCode (String ValidCode) {this.validCode = ValidCode; -ดำเนินการประมวลผลต่อไป
นำเข้า com.core.shared.validateCodeHandle; นำเข้า org.springframework.security.core.authentication; นำเข้า org.springframework.security.core.authenticationexception; org.springframework.security.web.util.matcher.antpathrequestmatcher; นำเข้า Javax.servlet.servletexception; นำเข้า Javax.servlet.http.httpservletRequest; javax.servlet.http.httpsession; นำเข้า java.io.ioexception; คลาสสาธารณะ MyValidCodeProcessingFilter ขยายบทคัดย่อ AbstractauthenticationProcessingFilter {สตริงส่วนตัวผู้ใช้ usernameparam = "ชื่อผู้ใช้"; สตริงรหัสผ่านส่วนตัว = "รหัสผ่าน"; สตริงส่วนตัว ValidCodeParam = "ValidateCode"; สาธารณะ myValidCodeProcessingFilter () {super (ใหม่ antpathrequestmatcher ("/ผู้ใช้/เข้าสู่ระบบ", "โพสต์")); } @Override การตรวจสอบความถูกต้องสาธารณะ retiveauthentication (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) พ่น AuthenticationException, iOexception, servletexception {String username = request.getParameter (usernameparam); String Password = request.getParameter (PasswordParam); String validCode = request.getParameter (ValidCodeParam); ถูกต้อง (ValidCode, request.getSession ()); myusernamepasswordauthenticationToken token = ใหม่ myusernamepasswordauthenticationToken (ชื่อผู้ใช้รหัสผ่าน ValidCode); ส่งคืนสิ่งนี้ GetAuthenticationManager (). Authenticate (โทเค็น); } โมฆะสาธารณะที่ถูกต้อง (String ValidCode, เซสชัน httpsession) {ถ้า (validCode == null) {โยน validCodeerRorexception ใหม่ ("ValidateCode ว่างเปล่า!"); } if (! validateCodeHandle.MatchCode (session.getId (), validCode)) {โยน validCodeerRorexception ใหม่ ("ValidCodeError!"); - กำหนดพารามิเตอร์สามตัวแยกกันซึ่งใช้เพื่อรับพารามิเตอร์จากแบบฟอร์มการเข้าสู่ระบบ วิธีการก่อสร้างให้ URL เข้าสู่ระบบและวิธีการโพสต์ที่จำเป็น
ถัดไปคือการรับรองความถูกต้อง ก่อนที่ชื่อผู้ใช้และรหัสผ่านการตรวจสอบจะมาถึงฉันเพิ่งตรวจสอบรหัสการตรวจสอบ
นี่คือคลาสเครื่องมือ ValidateCodeHandle และ ValidCodeerRorexception:
นำเข้า java.util.concurrent.concurrenthashmap; คลาสสาธารณะ ValidateCodeHandle {ส่วนตัวพร้อมกันพร้อมกันกับ chalidateCode = ใหม่พร้อมกันพร้อมกัน <> (); สาธารณะพร้อมกันพร้อมกันกับ getCode () {return validateCode; } โมฆะคงที่สาธารณะบันทึก (String SessionID, String Code) {validateCode.put (SessionId, รหัส); } สตริงคงที่ public String getValidateCode (String sessionId) {Object obj = validateCode.get (SessionId); if (obj! = null) {return string.valueof (obj); } return null; } public Static Boolean MatchCode (String SessionID, String InputCode) {String SavEcode = GetValIdateCode (SessionId); if (savecode.equals (inputCode)) {return true; } return false; -ที่นี่คุณต้องสืบทอดการรับรองความถูกต้องเพื่อระบุว่าเป็นความล้มเหลวของการตรวจสอบความปลอดภัยเพื่อให้กระบวนการล้มเหลวที่ตามมาจะถูกติดตาม
นำเข้า org.springframework.security.core.authenticationException; คลาสสาธารณะ ValidCodeerRorexception ขยายการรับรองความถูกต้อง Exception {Public ValidCodeerRorexception (String msg) {super (msg); } Public ValidCodeerRorexception (String msg, throwable t) {super (msg, t); -ถัดไปคือผู้ให้บริการ:
นำเข้า org.springframework.security.authentication.badcredentialsexception; นำเข้า org.springframework.security.authentication.usernamepasswordauthenticationToken นำเข้า org.springframework.security.authentication.dao.daoa org.springframework.security.core.authenticationexception; นำเข้า org.springframework.security.core.userdetails.userdetails; MyUserNamepasswordauthenticationToken.class.isassignable จาก (การตรวจสอบ); } @Override void void motelauthenticationchecks (ผู้ใช้ userdetails ผู้ใช้งาน, UserNamePasswordaThenticationToken Authentication) พ่น AuthenticationException {Object Salt = NULL; if (getSaltsource ()! = null) {salt = getSaltsource (). getSalt (userdetails); } if (Authentication.getCredentials () == null) {logger.debug ("การรับรองความถูกต้องล้มเหลว: ไม่มีข้อมูลรับรอง"); โยน badcredentialsexception ใหม่ ("ชื่อผู้ใช้หรือรหัสผ่านข้อผิดพลาด!"); } สตริงนำเสนอ password = Authentication.getCredentials (). toString (); if (! this.getPasswordEncoder (). iSpasswordValid (userDetails.getPassword (), password, salt)) {logger.debug ("การตรวจสอบความถูกต้องล้มเหลว: รหัสผ่านไม่ตรงกับค่าที่เก็บไว้"); โยน badcredentialsexception ใหม่ ("ภายใต้ชื่อผู้ใช้หรือรหัสผ่าน!"); - วิธีการสนับสนุนระบุการใช้โทเค็นที่กำหนดเองและวิธีการเพิ่มเติม uthenticationChecks นั้นเหมือนกับตรรกะของคลาสแม่ฉันเพิ่งเปลี่ยนข้อมูลที่ส่งคืนโดยข้อยกเว้น
ถัดไปคือตัวจัดการที่จัดการการรับรองความถูกต้องที่ประสบความสำเร็จและการรับรองความถูกต้องที่ล้มเหลว
นำเข้า org.springframework.security.core.authentication; นำเข้า org.springframework.security.web.authentication.simpleurlauthenticationsuccesshandler; นำเข้า javax.servlet.servletexception; javax.servlet.http.httpservletResponse; นำเข้า java.io.ioexception; ชั้นเรียนสาธารณะ frontauthenticationsuccesshandler ขยาย simpleurlauthenticationsuccesshandler {public frontauthenticationuccesshandler } @Override โมฆะสาธารณะ onauthenticationsuccess (คำขอ httpservletrequest, การตอบสนอง httpservletResponse, การตรวจสอบสิทธิ์การตรวจสอบสิทธิ์) พ่น IOException, servletexception {super.onauthenticationuccess - นำเข้า org.springframework.security.core.authenticationexception; นำเข้า org.springframework.security.web.authentication.simpleurlauthenticationfailurehandler; นำเข้า javax.servlet.servletexception; javax.servlet.http.httpservletResponse; นำเข้า java.io.ioexception; ชั้นเรียนสาธารณะ frontauthenticationFailureHandler ขยาย SimpleUrlauthenticationFailureHandler {public frontauthenticationFailureHandler } @Override โมฆะสาธารณะ onauthenticationFailure (คำขอ httpservletrequest, การตอบสนอง httpservletResponse, ข้อยกเว้นการรับรองความถูกต้อง exception) โยน ioexception, servletexception {super.onauthenticationFailure (คำขอ, การตอบสนอง, ยกเว้น); -ในที่สุดการกำหนดค่าความปลอดภัยที่สำคัญที่สุด:
นำเข้า com.service.user.customerservice; นำเข้า com.web.filter.sitemeshfilter; นำเข้า com.web.mysecurity.*; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.context org.springframework.context.annotation.configuration; นำเข้า org.springframework.core.annotation.order; นำเข้า org.springframework.security.authentication.authenticationManager; org.springframework.security.authentication.providerManager นำเข้า org.springframework.security.config.annotation.web.builders.httpsecurity; นำเข้า org.springframework.security.config.annotation. org.springframework.security.config.annotation.web.configuration.websecurityconfigureradapter นำเข้า org.springframework.security.crypto.password.passwordender; org.springframework.security.web.access.exceptiontranslationfilter; นำเข้า org.springframework.security.web.authentication.usernamepasswordauthenticationfilter นำเข้า org.springframework.security.web. org.springframework.web.filter.characterencodingfilter นำเข้า Javax.servlet.dispatchertype; นำเข้า Javax.servlet.filterregistration; นำเข้า Javax.servlet.servletContext; java.util.list;@configuration@enablewebsecuritypublic คลาส SecurityConfig ขยาย AbstractsecurityWebapplicationInitializer {@Bean รหัสผ่านสาธารณะรหัสผ่านสาธารณะ } @AutoWired ลูกค้าบริการลูกค้าบริการ; @Configuration @Order (1) คลาสสแตติกระดับ Public FrontendWebsecurityConfigureadapter ขยาย WebSecurityConfigurerAdapter {@autowired ส่วนตัว myValidCodeProcessingFilter MyValidCodeProcessingFilter; @Override Void Protected Configure (httpsecurity http) พ่นข้อยกเว้น {http.csrf (). disable () .authorizeRequests () .AntMatchers ("/ผู้ใช้/เข้าสู่ระบบ", "/ผู้ใช้/ล็อกอิน"). .addfilterbefore (myvalidcodeprocessingFilter, usernamepasswordauthenticationfilter.class) .formlogin () .loginpage ("/ผู้ใช้/ล็อกอิน"). และ () .logout () .logouturl ("/ผู้ใช้/ผู้ใช้") }} @Bean (name = "frontauthenticationProvider") สาธารณะ myauthenticationProvider frontauthenticationProvider () {myauthenticationProvider myauthenticationProvider = ใหม่ myauthenticationProvider (); MyAuthenticationProvider.SetUserDetailsService (ลูกค้าบริการ); MyAuthenticationProvider.SetPasswordEncoder (PasswordEncoder ()); ส่งคืน myauthenticationprovider; } @Bean Public AuthenticationManager AuthenticationManager () {รายการ <authenticationProvider> list = new ArrayList <> (); list.add (frontauthenticationProvider ()); AuthenticationManager AuthenticationManager = ใหม่ ProviderManager (รายการ); ส่งคืน AuthenticationManager; } @Bean สาธารณะ MyValidCodeProcessingFilter MyValidCodeProcessingFilter (AuthenticationManager AuthenticationManager) {MyValidCodeProcessingFilter ตัวกรอง = ใหม่ myValidCodeProcessingFilter (); Filter.SetAuthenticationManager (AuthenticationManager); Filter.setauthenticationsuccesshandler (frontauthenticationsuccesshandler ()); Filter.setAuthenticationFailureHandler (frontauthenticationFailureHandler ()); ตัวกรองส่งคืน; } @Bean Public FrontauthenticationFailureHandler FrontauthenticationFailureHandler () {ส่งคืน frontauthenticationFailureHandler ใหม่ ("/ผู้ใช้/เข้าสู่ระบบ"); } @Bean Public Frontauthenticationsuccesshandler FrontauthenticationsuccessHandler () {ส่งคืน frontauthenticationsuccesshandler ใหม่ ("/front/test"); } @Bean สาธารณะ myauthenticationEntryPoint myauthenticationEntryPoint () {ส่งคืน myauthenticationEntryPoint ("/ผู้ใช้/เข้าสู่ระบบ"); -ก่อนอื่นมันเป็นถั่วของคลาสการเข้ารหัสและลูกค้าบริการเป็นผู้ใช้แบบสอบถามง่ายๆ
@Service ("CustomerService") Public Class CustomerServiceImpl ดำเนินการลูกค้า {@AutoWired UserDao UserDao; @Override Public UserDetails LoadUserByUserName (String username) พ่น USERNAMENOTFoundException {ส่งคืน USERDAO.FINDCUSTOMERBYERNAME (ชื่อผู้ใช้); - ถัดไปคือ FrontendWebsecurityConfigureadapter ฉันเขียนวิธีการกำหนดค่าใหม่ปิดการใช้งาน CSRF ก่อนเปิดใช้งาน AuthorIzerequests () โดยที่ "/ผู้ใช้/เข้าสู่ระบบ" และ "/ผู้ใช้/ออกจากระบบ" ปล่อยการตรวจสอบสิทธิ์และคำขออื่น ๆ จำเป็นต้องมีการตรวจสอบล็อกอินแล้ว addFilterBefore () ก่อนที่จะเพิ่ม myvalidcodeprocessingFilter ที่กำหนดเองของฉันไปยัง usernamepasswordauthentication filter เริ่มต้นในการรักษาความปลอดภัยฉันทำการตรวจสอบพารามิเตอร์ที่กำหนดเองของฉันก่อนแล้ว formlogin () กำหนดค่า URL เข้าสู่ระบบ
ถัดไปคือการประกาศการแสดงผลถั่วของ AuthenticationProvider, AuthenticationManager, ProcessingFilter, AuthenticationFailureHandler, AuthenticationuccessHandler และ entrypoint
ด้านล่างคือ login.jsp
<body> <div> <ฟอร์ม: รูปแบบ autocomplete = "false" commandname = "userdto" method = "post"> <div> <pan> <b> $ {spring_security_last_exception.message} </b> </span> path = "รหัสผ่าน" cssclass = "form-control"/> <br/> validateCode: <ฟอร์ม: อินพุตพา ธ = "validateCode" cssclass = "form-control"/> <lable> $ {validate_code} value = "ส่ง"/> </ฟอร์ม: ฟอร์ม> </div> </body>เมื่อรหัสการตรวจสอบล้มเหลว ValidCodeerRorexception จะถูกโยนลงไป เนื่องจากมันสืบทอดการรับรองความถูกต้องเมื่อความปลอดภัยพบการรับรองความถูกต้อง Exception, AuthenticationFailureHandler จะถูกเรียกใช้ ถั่วข้างต้นประกาศว่าการรับรองความถูกต้องล้มเหลวและข้ามไปยัง URL เข้าสู่ระบบดังนั้นจึงมี $ {Spring_security_last_exception.message} ใน login.jsp ข้อมูลข้อยกเว้นจะถูกโยนทิ้งเมื่อฉันตรวจสอบสิทธิ์ซึ่งสามารถแจ้งให้ผู้ใช้เป็นมิตร
กระบวนการตรวจสอบความปลอดภัยที่กำหนดเองทั้งหมดเสร็จสมบูรณ์
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น