SpringSecurityOAuth2(5) custom login and logout

GitHub address

Code cloud address

Logout customization: Logout is equivalent to invalidating the token. We only need to carry the access_token to request ConsumerTokenServices (the default logout interface), and the old token will not be available after the request.

   @DeleteMapping("/logout")
    public ResponseVo logout(String accessToken) {
        if (consumerTokenServices.revokeToken(accessToken)) {
            return new ResponseVo(200, "登出成功");
        } else {
            return new ResponseVo(500, "登出失败");
        }
    }

Login customization: The default token request address is "oauth/token", we can customize the request address in the AuthorizationServerEndpointsConfigurer configuration of the authentication service configuration.

/**
     * 配置授权(authorization)以及令牌(token)的访问端点和令牌服务(token services)
     */
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//      endpoints.pathMapping("/oauth/token","/token/login");  设置token生成请求地址
        endpoints
                .tokenStore(tokenStore())  // 配置token存储
                .userDetailsService(userDetailsService)  // 配置自定义的用户权限数据,不配置会导致token无法刷新
                .authenticationManager(authenticationManager)
                .tokenServices(defaultTokenServices())// 加载token配置
                .exceptionTranslator(webResponseExceptionTranslator);  // 自定义异常返回
    }

We can also encapsulate a layer of our own request and then do some of our own processing before requesting the token. I use here to get the information that needs to request the token, and then use RestTemplate on the java side to call the method of generating the token address to get the token. I can do some other processing before the call, such as verification code verification and so on.

@PostMapping("/login")
 public ResponseVo login(HttpServletRequest request) throws UnsupportedEncodingException {
     String header = request.getHeader("Authorization");
     if (header == null && !header.startsWith("Basic")) {
         return new ResponseVo(400, "请求头中缺少参数");
     }
     String url = "http://" + request.getRemoteAddr() + ":" + request.getServerPort() + "/oauth/token";

     Map<String, Object> map = new HashMap<>();
     map.put("grant_type", "password");
     map.put("username", request.getParameter("username"));
     map.put("password", request.getParameter("password"));

     HttpHeaders headers = new HttpHeaders();
     headers.set("Authorization", header);
     headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);  // 必须该模式,不然请求端无法取到 grant_type

     HttpEntity httpEntity = new HttpEntity<>(headers);

     ResponseEntity<String> response = restTemplate.postForEntity(url + "?" + LinkStringUtil.createLinkStringByGet(map), httpEntity, String.class);

     if(response.getStatusCodeValue()==200){
         return new ResponseVo(200, "登录成功", JSONObject.parseObject(response.getBody()));
     }else{
         return new ResponseVo(500,"登录失败");
     }
 }
{{o.name}}
{{m.name}}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324133923&siteId=291194637