|
@@ -1,5 +1,6 @@
|
|
package com.flow.common.oauth2.configure;
|
|
package com.flow.common.oauth2.configure;
|
|
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Bean;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
@@ -34,11 +35,13 @@ import org.springframework.security.web.authentication.preauth.PreAuthenticatedA
|
|
|
|
|
|
import javax.sql.DataSource;
|
|
import javax.sql.DataSource;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
|
|
+import java.util.Date;
|
|
import java.util.HashMap;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
@Configuration
|
|
@Configuration
|
|
@EnableAuthorizationServer
|
|
@EnableAuthorizationServer
|
|
|
|
+@Slf4j
|
|
public class AuthorizationServerConfigure extends AuthorizationServerConfigurerAdapter {
|
|
public class AuthorizationServerConfigure extends AuthorizationServerConfigurerAdapter {
|
|
private final AuthenticationManager authenticationManager;
|
|
private final AuthenticationManager authenticationManager;
|
|
private final UserDetailsService userDetailsService;
|
|
private final UserDetailsService userDetailsService;
|
|
@@ -98,19 +101,21 @@ public class AuthorizationServerConfigure extends AuthorizationServerConfigurerA
|
|
@Override
|
|
@Override
|
|
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
|
|
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
|
|
endpoints
|
|
endpoints
|
|
- //认证管理器
|
|
|
|
|
|
+ // 认证管理器
|
|
.authenticationManager(authenticationManager)
|
|
.authenticationManager(authenticationManager)
|
|
- //用户信息
|
|
|
|
|
|
+ // 用户信息
|
|
.userDetailsService(userDetailsService)
|
|
.userDetailsService(userDetailsService)
|
|
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
|
|
.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
|
|
// 授权码管理策略,针对授权码模式有效,会将授权码放到 auth_code 表,授权后就会删除它
|
|
// 授权码管理策略,针对授权码模式有效,会将授权码放到 auth_code 表,授权后就会删除它
|
|
.authorizationCodeServices(jdbcAuthorizationCodeServices());
|
|
.authorizationCodeServices(jdbcAuthorizationCodeServices());
|
|
// 使用redis的token方式
|
|
// 使用redis的token方式
|
|
endpoints
|
|
endpoints
|
|
- //token存储
|
|
|
|
|
|
+ // token存储
|
|
.tokenStore(tokenStore())
|
|
.tokenStore(tokenStore())
|
|
// 添加令牌增强器
|
|
// 添加令牌增强器
|
|
.tokenEnhancer(customTokenEnhancer());
|
|
.tokenEnhancer(customTokenEnhancer());
|
|
|
|
+ // 明确地设置自定义的 tokenServices
|
|
|
|
+ //.tokenServices(tokenServices());
|
|
|
|
|
|
|
|
|
|
// 自定义异常转换类(替换oauth2默认返回格式,改成AjaxResponse统一格式)
|
|
// 自定义异常转换类(替换oauth2默认返回格式,改成AjaxResponse统一格式)
|
|
@@ -154,12 +159,19 @@ public class AuthorizationServerConfigure extends AuthorizationServerConfigurerA
|
|
tokenStore.removeRefreshToken(existingAccessToken.getRefreshToken());
|
|
tokenStore.removeRefreshToken(existingAccessToken.getRefreshToken());
|
|
}
|
|
}
|
|
}*/
|
|
}*/
|
|
|
|
+
|
|
|
|
+ log.info("使用了自定义的方法");
|
|
return super.createAccessToken(authentication);
|
|
return super.createAccessToken(authentication);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
- // 设置 Token 的有效期为一年(365 天 × 24 小时 × 60 分钟 × 60 秒)
|
|
|
|
- tokenServices.setAccessTokenValiditySeconds(365 * 24 * 60 * 60);
|
|
|
|
|
|
+ // 设置 Access Token 有效期为 1 分钟
|
|
|
|
+ // 默认Access Token 有效期:2分钟
|
|
|
|
+ // tokenServices.setAccessTokenValiditySeconds(60);
|
|
|
|
+
|
|
|
|
+ // 设置 Refresh Token 有效期为 365 天
|
|
|
|
+ // 默认Refresh Token 有效期:7天
|
|
|
|
+ // tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 365);
|
|
|
|
|
|
// 请求回来的token都会变.但是请求的refresh token不会续期(false)或者重置为初始化时间(true)
|
|
// 请求回来的token都会变.但是请求的refresh token不会续期(false)或者重置为初始化时间(true)
|
|
tokenServices.setReuseRefreshToken(true);
|
|
tokenServices.setReuseRefreshToken(true);
|
|
@@ -167,8 +179,8 @@ public class AuthorizationServerConfigure extends AuthorizationServerConfigurerA
|
|
tokenServices.setSupportRefreshToken(true);
|
|
tokenServices.setSupportRefreshToken(true);
|
|
tokenServices.setClientDetailsService(clientDetailsService);
|
|
tokenServices.setClientDetailsService(clientDetailsService);
|
|
// token加强链
|
|
// token加强链
|
|
- //TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
|
|
|
|
- //tokenEnhancerChain.setTokenEnhancers(Lists.newArrayList(tokenEnhancer()));
|
|
|
|
|
|
+ // TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
|
|
|
|
+ // tokenEnhancerChain.setTokenEnhancers(Lists.newArrayList(tokenEnhancer()));
|
|
tokenServices.setTokenEnhancer(customTokenEnhancer());
|
|
tokenServices.setTokenEnhancer(customTokenEnhancer());
|
|
// token存储
|
|
// token存储
|
|
tokenServices.setTokenStore(tokenStore());
|
|
tokenServices.setTokenStore(tokenStore());
|
|
@@ -180,6 +192,9 @@ public class AuthorizationServerConfigure extends AuthorizationServerConfigurerA
|
|
list.add(provider);
|
|
list.add(provider);
|
|
tokenServices.setAuthenticationManager(new ProviderManager(list));
|
|
tokenServices.setAuthenticationManager(new ProviderManager(list));
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // tokenServices.setAccessTokenValiditySeconds(60);
|
|
|
|
+ log.info("tokenServices: {}", tokenServices);
|
|
return tokenServices;
|
|
return tokenServices;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -199,6 +214,8 @@ public class AuthorizationServerConfigure extends AuthorizationServerConfigurerA
|
|
additionalInformation.put("clientId", authentication.getOAuth2Request().getClientId());
|
|
additionalInformation.put("clientId", authentication.getOAuth2Request().getClientId());
|
|
additionalInformation.put("resourceIds", authentication.getOAuth2Request().getResourceIds());
|
|
additionalInformation.put("resourceIds", authentication.getOAuth2Request().getResourceIds());
|
|
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInformation);
|
|
((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInformation);
|
|
|
|
+ // 设置过期时间
|
|
|
|
+ ((DefaultOAuth2AccessToken) accessToken).setExpiration(new Date(System.currentTimeMillis() + 30 * 1000));
|
|
}
|
|
}
|
|
return accessToken;
|
|
return accessToken;
|
|
}
|
|
}
|