首页 > 编程知识 正文

accesstoken和refreshtoken,oauth2资源服务器校验token

时间:2023-05-05 05:16:29 阅读:130753 作者:3127

令牌更新过程涉及令牌端点org.spring framework.security.oauth2. provider.endpoint.token endpoint @ request mapping (vvvview oauth ) )进行更新method=request method.post (publicresponseentityoauth2accesstokenpostaccesstoken (原始打印机方法) @ requestparammapstoken string parameters (throwshttprequestmethodnotsupportedexception (…if ) isrefreshqsdxhd ) parameters ) arefreshtoken oweshouldignoreanyaddedbythefactoryhere.token request.setscope (oauth2utils.parseparameterlist (参数新令牌oauth 2访问权限=gettokengranter (.grant ) tokenRequest.getgranttype ),tokenrequest ); if(token==null ) thrownewunsupportedgranttypeexception (' unsupportedgranttype ); }返回获取响应(token ); 根据grant_type=refresh_token,拿到奇怪的长筒袜, tokengranter.grantrefreshtokengranterorg.spring framework.security.oauth2. provider.refresh.refreshtokengranteranterproror .@ overrideprotectedoauth2accesstokengetaccesstoken {积极老师,qsdxhdtokenrequest } { stringrefreshtoken }返回获取服务}} DefaultTokenServices通过refresh_token获取凭据并创建预验证token。 以预认证格式访问user details org.spring framework.security.oauth2. provider.token.defaulttokenservices @ transactional (nal bace ) invalidgrantexception.class } (publico auth2accesstokenrefreshaccesstoken (stringrefreshtokenvalue,qsd xhd令牌请求) ) 支持refreshtoken (thrownewinvalidgrantexception (invalidrefreshtoken : ) refreshtokenvalue ); } oauth2refreshtokenrefreshtoken=token store.readrefreshtoken (refreshtokenvalue ); if (refresh token==null (thrownewinvalidgrantexception (invalidrefreshtoken : ) Refreshtokenvalue ); } oauth2authenticationauthentication=token store.readauthenticationforrefreshtoken (refresh token ); if(this.authenticationmanager!=空! authentication.isClientOnly () (/theclienthasalreadybeenauthenticated,buttheuserauthenticationmightbeoldnow, sogi veita//chance tore-authenticate.//在此创建预验证token,然后在authenticationManager中验证此token以获取用户身份验证cationToken(authentication.getUserAuthentication(), "", authentication.getAuthorities());user = authenticationManager.authenticate(user);Object details = authentication.getDetails();authentication = new OAuth2Authentication(authentication.getOAuth2Request(), user);authentication.setDetails(details);}String clientId = authentication.getOAuth2Request().getClientId();if (clientId == null || !clientId.equals(tokenRequest.getClientId())) {throw new InvalidGrantException("Wrong client for this refresh token: " + refreshTokenValue);}// clear out any access tokens already associated with the refresh// token.tokenStore.removeAccessTokenUsingRefreshToken(refreshToken);if (isExpired(refreshToken)) {tokenStore.removeRefreshToken(refreshToken);throw new InvalidTokenException("Invalid refresh token (expired): " + refreshToken);}authentication = createRefreshedAuthentication(authentication, tokenRequest);if (!reuseRefreshToken) {tokenStore.removeRefreshToken(refreshToken);refreshToken = createRefreshToken(authentication);}OAuth2AccessToken accessToken = createAccessToken(authentication, refreshToken);tokenStore.storeAccessToken(accessToken, authentication);if (!reuseRefreshToken) {tokenStore.storeRefreshToken(accessToken.getRefreshToken(), authentication);}return accessToken;}

5.PreAuthenticatedAuthenticationProvider 获取userdetails

org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider public Authentication authenticate(Authentication authentication)throws AuthenticationException {...UserDetails ud = preAuthenticatedUserDetailsService.loadUserDetails((PreAuthenticatedAuthenticationToken) authentication);userDetailsChecker.check(ud);PreAuthenticatedAuthenticationToken result = new PreAuthenticatedAuthenticationToken(ud, authentication.getCredentials(), ud.getAuthorities());result.setDetails(authentication.getDetails());return result;} 默认配置情况 默认tokenServices配置 org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer private AuthorizationServerTokenServices tokenServices() {if (tokenServices != null) {return tokenServices;}this.tokenServices = createDefaultTokenServices();return tokenServices;}public AuthorizationServerTokenServices getDefaultAuthorizationServerTokenServices() {if (defaultTokenServices != null) {return defaultTokenServices;}this.defaultTokenServices = createDefaultTokenServices();return this.defaultTokenServices;}private DefaultTokenServices createDefaultTokenServices() {DefaultTokenServices tokenServices = new DefaultTokenServices();tokenServices.setTokenStore(tokenStore());tokenServices.setSupportRefreshToken(true);tokenServices.setReuseRefreshToken(reuseRefreshToken);tokenServices.setClientDetailsService(clientDetailsService());tokenServices.setTokenEnhancer(tokenEnhancer());addUserDetailsService(tokenServices, this.userDetailsService);return tokenServices;} 默认PreAuthenticatedAuthenticationProvider配置 org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer private ClientDetailsService clientDetailsService() {if (clientDetailsService == null) {this.clientDetailsService = new InMemoryClientDetailsService();}if (this.defaultTokenServices != null) {addUserDetailsService(defaultTokenServices, userDetailsService);}return this.clientDetailsService;}private void addUserDetailsService(DefaultTokenServices tokenServices, UserDetailsService userDetailsService) {if (userDetailsService != null) {PreAuthenticatedAuthenticationProvider provider = new PreAuthenticatedAuthenticationProvider();provider.setPreAuthenticatedUserDetailsService(new UserDetailsByNameServiceWrapper<PreAuthenticatedAuthenticationToken>(userDetailsService));tokenServices.setAuthenticationManager(new ProviderManager(Arrays.<AuthenticationProvider> asList(provider)));}} 默认UserDetailsService配置 org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerSecurityConfiguration @Overrideprotected void configure(HttpSecurity http) throws Exception {...// 如果没有配置UserDetailsServiceif (!endpoints.getEndpointsConfigurer().isUserDetailsServiceOverride()) {UserDetailsService userDetailsService = http.getSharedObject(UserDetailsService.class);endpoints.getEndpointsConfigurer().userDetailsService(userDetailsService);}...} 解决方案

3种方案选一即可

AuthorizationServer增加配置UserDetailsService @Configuration@EnableAuthorizationServerpublic class sdddc extends sdddcurerAdapter {@Autowired UserDetailsService userDetailsService;... @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { ... endpoints.userDetailsService(userDetailsService); ... } ...} WebSecurity将UserDetailsService注入AuthorizationServerEndpointsConfigurer
此方案需要sdddc 的Order高ssdl,否则无法注入 @Autowiredprivate AuthorizationServerEndpointsConfiguration endpoints;@Overrideprotected void configure(HttpSecurity http) throws Exception { if (!endpoints.getEndpointsConfigurer().isUserDetailsServiceOverride()) { UserDetailsService userDetailsService = http.getSharedObject(UserDetailsService.class); endpoints.getEndpointsConfigurer().userDetailsService(userDetailsService); } ...} AuthorizationServer增加配置自定义TokenService

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。