บทความนี้แนะนำ Spring Security OAuth2.0 เพื่อใช้ตัวอย่างการเข้าสู่ระบบรหัสการตรวจสอบ SMS และแบ่งปันกับคุณดังนี้:
กำหนดโทเค็นหมายเลขโทรศัพท์มือถือเข้าสู่ระบบ
/** * @author length * @date 2018/1/9 * โทเค็นหมายเลขโทรศัพท์มือถือ */คลาสสาธารณะ MobileAuthenticationToken ขยายบทคัดย่อ AbstractauthenticationToken {ส่วนตัวคงที่สุดท้าย อาจารย์ใหญ่วัตถุสุดท้ายส่วนตัว; สาธารณะ mobileAuthenticationToken (สตริงมือถือ) {super (null); this.principal = มือถือ; setauthenticated (เท็จ); } สาธารณะ MobileAuthenticationToken (วัตถุอาจารย์ใหญ่, คอลเลกชัน < this.principal = principal; super.setauthenticated (จริง); } วัตถุสาธารณะ getPrincipal () {return this.principal; } @Override วัตถุสาธารณะ getCredentials () {return null; } โมฆะสาธารณะ setauthenticated (บูลีน isauthenticated) พ่น unledalargumentexception {ถ้า (isauthenticated) {โยน unlegalargumentException ใหม่ ("ไม่สามารถตั้งโทเค็นนี้ให้เชื่อถือได้ - ใช้ตัวสร้าง } super.setauthenticated (เท็จ); } @Override โมฆะสาธารณะ erasecredentials () {super.erasecredentials (); -หมายเลขโทรศัพท์มือถือลอจิกการตรวจสอบ
/** * @author length * @date 2018/1/9 * หมายเลขมือถือการตรวจสอบลอจิกการตรวจสอบ */คลาสสาธารณะ MobileAuthenticationProvider ใช้การตรวจสอบความถูกต้อง Provider {ผู้ใช้ส่วนตัวผู้ใช้บริการ; @Override การตรวจสอบความถูกต้องสาธารณะ (การรับรองความถูกต้องของการรับรองความถูกต้อง) พ่น AuthenticationException {MobileAuthEnticationToken MobileAuthenticationToken = (MobileAuthenticationToken) การรับรองความถูกต้อง; uservo uservo = userservice.finduserbymobile ((String) MobileAuthenticationToken.getPrincipal ()); userDetailSimpl ผู้ใช้งาน = buildUserDeatils (uservo); if (userDetails == null) {โยน InternalauthenticationserviceException ใหม่ ("หมายเลขโทรศัพท์มือถือไม่มีอยู่:" + MobileAuthenticationToken.getPrincipal ()); } MobileAuthenticationToken AuthenticationToken = ใหม่ MobileAuthenticationToken (ผู้ใช้งาน, userDetails.getAuthorities ()); AuthenticationToken.setDetails (MobileAuthenticationToken.getDetails ()); ส่งคืน AuthenticationToken; } UserDetailSimpl buildUserDeatils (USERVO USERVO) {ส่งคืน USERDETAILSIMPL ใหม่ (USERVO); } @Override บูลีนสาธารณะสนับสนุน (คลาส <?> การรับรองความถูกต้อง) {ส่งคืน mobileAuthenticationToken.class.isassignable from (การตรวจสอบ); } Public Userservice GetUserservice () {return userservice; } โมฆะสาธารณะ SetUsERService (UserserVice UserserVice) {this.userService = Userservice; -การประมวลผลตัวกรองกระบวนการเข้าสู่ระบบ
/** * @author length * @date 2018/1/9 * หมายเลขมือถือการตรวจสอบตัวกรองการเข้าสู่ระบบ */คลาสสาธารณะ MobileAuthenticationFilter ขยาย AbstractauthenticationProcessingFilter {สาธารณะคงที่สตริงสุดท้าย Spring_Security_form_mobile_key = "มือถือ"; สตริงส่วนตัว mobileparameter = spring_security_form_mobile_key; บูลีนส่วนตัว postonly = true; Public MobileAuthenticationFilter () {Super (ใหม่ antpathRequestMatcher (SecurityConstants.mobile_token_url, "Post")); } การตรวจสอบความถูกต้องสาธารณะ retedauthentication (คำขอ httpservletrequest, httpservletResponse การตอบสนอง) พ่นการรับรองความถูกต้อง exception {ถ้า (postonly &&! request.getMethod (). เท่ากับ (httpmethod.post.name ())) } String mobile = attainMobile (คำขอ); if (mobile == null) {mobile = ""; } mobile = mobile.trim (); MobileAuthenticationToken MobileAuthenticationToken = ใหม่ MobileAuthenticationToken (มือถือ); SetDetails (คำขอ, MobileAuthenticationToken); ส่งคืนสิ่งนี้ GetAuthenticationManager (). Authenticate (MobileAuthenticationToken); } การป้องกันสตริงที่ได้รับ Mobile (คำขอ httpservletRequest) {return request.getParameter (mobileparameter); } การป้องกันโมฆะ setDetails (คำขอ httpservletRequest, mobileAuthenticationToken AuthRequest) {AuthRequest.setDetails (AuthenticationDetailsSource.buildDetails (คำขอ)); } โมฆะสาธารณะ setPostonly (บูลีน postonly) {this.postonly = postonly; } สตริงสาธารณะ getMobileparameter () {return mobileparameter; } โมฆะสาธารณะ setMobileparameter (สตริง mobileparameter) {this.mobileparameter = mobileparameter; } บูลีนสาธารณะ ispostonly () {return postonly; -ตำแหน่งโทเค็นการผลิต
/** * ความยาว @author * @date 2018/1/8 * การเข้าสู่ระบบหมายเลขโทรศัพท์มือถือสำเร็จแล้วและส่งคืนโทเค็น Oauth */ @componentPublic คลาส mobileloginsuccesshandler ใช้ org.springframework.security.web.authentication.authentication @autowired ObjectMapper ObjectMapper; @autowired Private ClientDetailsService ClientDetailsService; @AutoWired AuthorizationServervertoKenServices AuthorizationServervertokenServices; @Override โมฆะสาธารณะ onauthenticationsuccess (คำขอ httpservletRequest, การตอบสนอง httpservletResponse, การตรวจสอบสิทธิ์การตรวจสอบสิทธิ์) {header string = request.getheader ("การอนุญาต"); if (header == null ||! header.startswith ("basic"))) {โยน unapprovedclientauthenticationException ใหม่ ("ข้อมูลลูกค้าในส่วนหัวคำขอว่างเปล่า"); } ลอง {string [] tokens = extractandDecodeHeader (ส่วนหัว); ยืนยันโทเค็นความยาว == 2; String clientId = โทเค็น [0]; String clientsecret = โทเค็น [1]; jsonObject params = new jsonObject (); params.put ("clientId", clientId); params.put ("clientsecret", clientsecret); params.put ("การรับรองความถูกต้อง", การตรวจสอบ); clientDetails clientDetails = clientDetailsService.loadClientByClientIdid (clientId); tokenrequest tokenrequest = new tokenrequest (maputil.newhashmap (), clientId, clientDetails.getScope (), "มือถือ"); OAuth2Request OAuth2Request = tokenrequest.createoauth2Request (clientDetails); OAuth2Authentication OAuth2Authentication = ใหม่ OAuth2Authentication (OAuth2Request, การตรวจสอบ); OAuth2AccessToken OAuth2accessToken = AuthorizationServervertokenServices.CreateAccessToken (OAuth2Authentication); logger.info ("รับโทเค็นประสบความสำเร็จ: {}", oauth2accessToken.getValue ()); Response.Setcharacterencoding (CommonConstant.UTF8); Response.SetContentType (CommonConstant.Content_Type); PrintWriter PrintWriter = Response.getWriter (); PrintWriter.Append (ObjectMapper.WriteValueEasString (OAuth2accessToken)); } catch (ioexception e) {โยน badcredentientSexception ใหม่ ("ไม่สามารถถอดรหัสโทเค็นการตรวจสอบความถูกต้องพื้นฐาน"); }} /*** ถอดรหัสส่วนหัวเป็นชื่อผู้ใช้และรหัสผ่าน * * @throws badcredentialsexception หากส่วนหัวพื้นฐานไม่ปรากฏหรือไม่ถูกต้อง * base64 */ สตริงส่วนตัว [] extractandDecodeHeader (ส่วนหัวสตริง) พ่น IOException {byte [] base64Token = head.substring (6) .getByTes ("UTF-8"); ไบต์ [] ถอดรหัส; ลอง {decoded = base64.decode (base64token); } catch (unlegalArgumentException e) {โยน badcredentientSexception ใหม่ ("ล้มเหลวในการถอดรหัสโทเค็นการตรวจสอบขั้นพื้นฐาน"); } สตริงโทเค็น = สตริงใหม่ (ถอดรหัส, CommonConstant.UTF8); int delim = token.indexof (":"); if (delim == -1) {โยน badCredentialSexception ใหม่ ("โทเค็นการตรวจสอบขั้นพื้นฐานที่ไม่ถูกต้อง"); } ส่งคืนสตริงใหม่ [] {token.substring (0, delim), token.substring (delim + 1)}; -กำหนดค่าการปรับแต่งด้านบน
// ** * @author ความยาว * @date 2018/1/9 * หมายเลขโทรศัพท์มือถือการลงชื่อเข้าใช้พอร์ทัล */ @ComponentPublic คลาส MobilesecurityConfigurer ขยาย SecurityConfigurerAdapter <ค่าเริ่มต้น @AutoWired Userservice Userservice; @Override โมฆะสาธารณะกำหนดค่า (httpsecurity http) โยนข้อยกเว้น {mobileauthenticationfilter mobileauthenticationFilter = ใหม่ mobileAuthenticationFilter (); MobileAuthenticationFilter.SetAuthenticationManager (http.getsharedObject (AuthenticationManager.class)); MobileAuthenticationFilter.SetauthenticationsuccessHandler (MobileLoginSuccessHandler); MobileAuthenticationProvider MobileAuthenticationProvider = ใหม่ MobileAuthenticationProvider (); MobileAuthenticationProvider.SetUsERService (ผู้ใช้บริการ); http.authenticationprovider (MobileAuthenticationProvider) .addfilterafter (MobileAuthenticationFilter, UserNamePasswordAuthenticationFilter.class); -การกำหนดค่ารวมที่ระบุไว้ข้างต้นการกำหนดค่าความปลอดภัยของสปริง
/** * ความยาว @author * @date 9 มกราคม 2018 14:01:25 * เซิร์ฟเวอร์การตรวจสอบความถูกต้องเปิดการกำหนดค่าอินเตอร์เฟส */ @การกำหนดค่า @enableeresourceserverpublic class resourceserverconfiguration @Autowired Private MobileSecurityConfigurer MobileSecurityCurityCurityCurityCurityCurityCurityCurityCurityCurityConforcurity; @Override โมฆะสาธารณะกำหนดค่า (httpsecurity http) โยนข้อยกเว้น {registry .AntMatchers ("/มือถือ/โทเค็น"). Permissionall () .anyRequest (). รับรองความถูกต้อง (). และ () .csrf (). disable (); http.apply (MobilesecurityConfigurer); -ใช้
การคัดลอกรหัสมีดังนี้:
curl -h "การอนุญาต: cglnonbpzw พื้นฐาน ==" -d "grant_type = mobile & scope = เซิร์ฟเวอร์และมือถือ = 17034642119 & code =" http: // localhost: 9999/auth/mobile/token
รหัสต้นฉบับ
โปรดดูที่ gitee.com/log4j/
ขึ้นอยู่กับ OAuth2.0 ความปลอดภัยในสปริงคลาวด์และฤดูใบไม้ผลิการตรวจสอบความถูกต้องระดับองค์กรและการอนุญาตได้รับการพัฒนาโดยให้การตรวจสอบบริการทั่วไปการติดตามลิงก์การวิเคราะห์บันทึกการจัดการแคชการจัดตารางงานและการใช้งานอื่น ๆ
ตรรกะทั้งหมดถูกนำไปใช้ในโหมดการเข้าสู่ระบบของผู้ใช้งานของความปลอดภัยของสปริงและคุณสามารถอ้างอิงถึงซอร์สโค้ด
ตรรกะของการออกและตรวจสอบรหัสการตรวจสอบนั้นค่อนข้างง่าย หลังจากวิธีการให้ใช้ Global Fitter เพื่อตรวจสอบว่ารหัสในคำขอตรงกับหมายเลขโทรศัพท์มือถือหรือไม่ ตรรกะคีย์คือพารามิเตอร์ของโทเค็น
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น