# 实验四 基于Spring Security码云OAuth2认证的实验仓库 **Repository Path**: chainsawman/spring-security-gitee-experiment-4 ## Basic Information - **Project Name**: 实验四 基于Spring Security码云OAuth2认证的实验仓库 - **Description**: 实验四 基于Spring Security码云OAuth2认证的实验仓库 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 70 - **Created**: 2020-12-06 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 实验四 基于Spring Security码云OAuth2认证的实验仓库 # Springboot实验四 | 实验名称: | 利用Spring boot的自动装配特性实现动态注册组件 | 实验序号: | 四 | |-------|---------------------|-------|--------------| | 姓名: | 陈志聪 | 学号: | 201841413203 | | 班级: | 18网工2班 | 实验地点: | 线上 | | 实验日期: | 2020/11/20 | 指导老师: | 黎志雄 | | 成绩: | | 同组同学: | 无 | ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/214558_6a00f210_8165504.png "屏幕截图.png") ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/214623_74480969_8165504.png "屏幕截图.png") ``` public Authentication authenticate(Authentication authentication) throws AuthenticationException { // 用户认证前,构造一个GiteeOAuth2LoginAuthenticationToken GiteeOAuth2LoginAuthenticationToken authenticationToken = (GiteeOAuth2LoginAuthenticationToken) authentication; // 通过码云API获取access_token String accessToken = getAccessToken(authenticationToken.getCode()); // 通过码云API获取Gitee授权用户的资料 Map userInfo = getUserInfo(accessToken); // 认证成功后,重新生成Authentication return createSuccessAuthentication(Objects.requireNonNull(userInfo), authenticationToken.getRequest()); } ``` ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/214922_331acbc9_8165504.png "屏幕截图.png") ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/214959_10605f4e_8165504.png "屏幕截图.png") ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/215010_49952e51_8165504.png "屏幕截图.png") ``` if (!request.getRequestURI().endsWith(DEFAULT_AUTHORIZATION_REQUEST_BASE_URI)) { filterChain.doFilter(request, response); return;// 执行完安全过滤器链后不执行后面的代码。 } String uriString = UriComponentsBuilder.fromUriString("https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code") .encode() .buildAndExpand(CLIENT_ID,REDIRECT_URI).toUriString(); response.sendRedirect(uriString); ``` ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/215043_e2ee1155_8165504.png "屏幕截图.png") ``` URI uri=UriComponentsBuilder.fromUriString("https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}") .encode() .buildAndExpand(code,CLIENT_ID,REDIRECT_URI,CLIENT_SECRET).toUri(); RestTemplate rest=new RestTemplate(); RequestEntity requestEntity=RequestEntity.post(uri).header("User-Agent","Dgut Demo").build(); ResponseEntity exchange = rest.exchange(requestEntity, String.class); String result=exchange.getBody(); Map resultMap = new JacksonJsonParser().parseMap(result); String access_token=(String)resultMap.get("access_token"); System.out.println(access_token); return access_token; ``` ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/215118_ca85ccfa_8165504.png "屏幕截图.png") ``` URI uri=UriComponentsBuilder.fromUriString(USER_INFO_URI) .encode() .buildAndExpand(accessToken) .toUri(); RestTemplate rest=new RestTemplate(); RequestEntity requestEntity=RequestEntity.get(uri).header("User-Agent","Dgut_Demo2").build(); ResponseEntity exchange = rest.exchange(requestEntity, String.class); String result=exchange.getBody(); Map resultMap = new JacksonJsonParser().parseMap(result); return resultMap; ``` ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/215232_6d116e96_8165504.png "屏幕截图.png") ``` http.addFilterAfter(new GiteeOAuth2RedirectFilter(), LogoutFilter.class); http.addFilterAfter(new GiteeOAuth2LoginAuthenticationFilter(),LogoutFilter.class); ``` ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/215312_4025f4e2_8165504.png "屏幕截图.png") ``` http.apply(new GiteeOAuth2LoginConfigurer<>()); ``` ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/215401_3a4d35b5_8165504.png "屏幕截图.png") ``` String userIndex(Authentication auth,Model model) { System.out.println(auth.getName()); String username=auth.getName(); model.addAttribute("username",username); return "user"; } ``` ![输入图片说明](https://images.gitee.com/uploads/images/2020/1208/215428_ad9d985f_8165504.png "屏幕截图.png") ``` @Test @WithMockUser(authorities = "USER") @GetMapping("/test") public void test() throws Exception { String naive= mvc.perform(get("/test")).andExpect(status().isOk()).andDo(print()).andReturn().getResponse().getContentAsString(); System.out.println(naive); } ``` ![输入图片说明](https://images.gitee.com/uploads/images/2020/1209/122747_1876de8d_8165504.png "屏幕截图.png")