注意:刷新令牌只有在授权码模式和密码模式中才有,对应的指定这两种模式时,在类型上加上refresh_token
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
<version>2.3.12.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.security.oauthgroupId>
<artifactId>spring-security-oauth2artifactId>
<version>2.3.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.3.12.RELEASEversion>
dependency>
@Configuration
public class MyOAuth2Config {
/**
* 加密方式
*/
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
/**
* 当前需要使用内存方式存储了用户令牌,应当使用UserDetailsService才行,否则会报错
*/
@Component
public class MyUserDetailService implements UserDetailsService {
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
return new User("admin", passwordEncoder.encode("123456"),
AuthorityUtils.commaSeparatedStringToAuthorityList("admin_role"));
}
}
@EnableWebSecurity
public class OAuth2SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyUserDetailService myUserDetailService;
/**
* password密码模式要使用此认证管理器
*/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* 用户类信息
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myUserDetailService);
}
}
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private MyUserDetailService myUserDetailService;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("test-pc")
.secret(passwordEncoder.encode("123456"))
.resourceIds("oauth2-server")
.authorizedGrantTypes("password", "authorization_code", "refresh_token")
.scopes("all")
.autoApprove(false)
.redirectUris("http://www.baidu.com/");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//密码模式需要配置认证管理器
endpoints.authenticationManager(authenticationManager);
//刷新令牌获取新令牌时需要
endpoints.userDetailsService(myUserDetailService);
}
}
TokenStore接口负责持久化令牌,默认情况下,令牌是通过randomUUID产生的32位随机数来进行填充,从而产生的令牌默认是存储在内存中
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
<version>2.3.12.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.security.oauthgroupId>
<artifactId>spring-security-oauth2artifactId>
<version>2.3.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.3.12.RELEASEversion>
dependency>
@Configuration
public class MyOAuth2Config {
@Bean
public TokenStore tokenStore(){
return new InMemoryTokenStore();
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
/**
* 当前需要使用内存方式存储了用户令牌,应当使用UserDetailsService才行,否则会报错
*/
@Component
public class MyUserDetailService implements UserDetailsService {
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
return new User("admin", passwordEncoder.encode("123456"),
AuthorityUtils.commaSeparatedStringToAuthorityList("admin_role"));
}
}
@EnableWebSecurity
public class OAuth2SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyUserDetailService myUserDetailService;
/**
* password密码模式要使用此认证管理器
*/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* 用户类信息
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myUserDetailService);
}
}
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private MyUserDetailsService myUserDetailsService;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private TokenStore tokenStore;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("test-pc")
.secret(passwordEncoder.encode("123456"))
.resourceIds("oauth2-server")
.authorizedGrantTypes("authorization_code", "password", "implicit", "client_credentials", "refresh_token")
.scopes("all")
.autoApprove(false)
.redirectUris("http://www.baidu.com");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
endpoints.userDetailsService(myUserDetailsService);
//令牌管理策略
endpoints.tokenServices(tokenService());
}
@Bean
public AuthorizationServerTokenServices tokenService() {
DefaultTokenServices service=new DefaultTokenServices();
//service.setClientDetailsService();//客户端详情服务
service.setSupportRefreshToken(true);//支持刷新令牌
service.setTokenStore(tokenStore);//令牌存储策略
service.setAccessTokenValiditySeconds(7200); // 令牌默认有效期2小时
service.setRefreshTokenValiditySeconds(259200); // 刷新令牌默认有效期3天
return service;
}
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
<version>2.3.12.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.security.oauthgroupId>
<artifactId>spring-security-oauth2artifactId>
<version>2.3.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.3.12.RELEASEversion>
dependency>
@Configuration
public class MyOAuth2Config {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Bean
public TokenStore redisTokenStore(){
// redis管理令牌
return new RedisTokenStore(redisConnectionFactory);
}
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
}
/**
* 当前需要使用内存方式存储了用户令牌,应当使用UserDetailsService才行,否则会报错
*/
@Component
public class MyUserDetailService implements UserDetailsService {
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
return new User("admin", passwordEncoder.encode("123456"),
AuthorityUtils.commaSeparatedStringToAuthorityList("admin_role"));
}
}
@EnableWebSecurity
public class OAuth2SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyUserDetailService myUserDetailService;
/**
* password密码模式要使用此认证管理器
*/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* 用户类信息
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myUserDetailService);
}
}
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private MyUserDetailsService myUserDetailsService;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private TokenStore tokenStore;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("test-pc")
.secret(passwordEncoder.encode("123456"))
.resourceIds("oauth2-server")
.authorizedGrantTypes("authorization_code", "password", "implicit", "client_credentials", "refresh_token")
.scopes("all")
.autoApprove(false)
.redirectUris("http://www.baidu.com");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
endpoints.userDetailsService(myUserDetailsService);
//令牌管理策略
endpoints.tokenStore(tokenStore);
}
}
具体SQL语句可以去官网查看
-- used in tests that use HSQL
create table oauth_client_details (
client_id VARCHAR(128) PRIMARY KEY,
resource_ids VARCHAR(256),
client_secret VARCHAR(256),
scope VARCHAR(256),
authorized_grant_types VARCHAR(256),
web_server_redirect_uri VARCHAR(256),
authorities VARCHAR(256),
access_token_validity INTEGER,
refresh_token_validity INTEGER,
additional_information VARCHAR(4096),
autoapprove VARCHAR(256)
);
INSERT INTO `oauth_client_details` VALUES ('test-pc', 'oauth2-server,oauth2-resource', '$2a$10$Q2Dv45wFHgxQkFRaVNAzeOJorpTH2DwHb975VeHET30QsqwuoQOAe', 'all,Base_API', 'authorization_code,password,implicit,client_credentials,refresh_token', 'http://www.baidu.com/', NULL, 50000, NULL, NULL, 'false');
create table oauth_client_token (
token_id VARCHAR(256),
token BLOB,
authentication_id VARCHAR(256) PRIMARY KEY,
user_name VARCHAR(256),
client_id VARCHAR(256)
);
create table oauth_access_token (
token_id VARCHAR(256),
token BLOB,
authentication_id VARCHAR(256) PRIMARY KEY,
user_name VARCHAR(256),
client_id VARCHAR(256),
authentication BLOB,
refresh_token VARCHAR(256)
);
create table oauth_refresh_token (
token_id VARCHAR(256),
token BLOB,
authentication BLOB
);
create table oauth_code (
code VARCHAR(256),
authentication BLOB
);
create table oauth_approvals (
userId VARCHAR(256),
clientId VARCHAR(256),
scope VARCHAR(256),
status VARCHAR(10),
expiresAt TIMESTAMP,
lastModifiedAt TIMESTAMP
);
-- customized oauth_client_details table
create table ClientDetails (
appId VARCHAR(256) PRIMARY KEY,
resourceIds VARCHAR(256),
appSecret VARCHAR(256),
scope VARCHAR(256),
grantTypes VARCHAR(256),
redirectUrl VARCHAR(256),
authorities VARCHAR(256),
access_token_validity INTEGER,
refresh_token_validity INTEGER,
additionalInformation VARCHAR(4096),
autoApproveScopes VARCHAR(256)
);
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
dependency>
<dependency>
<groupId>org.springframework.security.oauthgroupId>
<artifactId>spring-security-oauth2artifactId>
<version>2.3.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.4.3.4version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.8version>
dependency>
server:
port: 8080
spring:
application:
name: oauth2-server
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/oauth2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
@Configuration
public class MyOauth2Config {
/**
* druid数据源
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource() {
return new DruidDataSource();
}
/**
* jdbc管理令牌
*/
@Bean
public TokenStore jdbcTokenStore() {
return new JdbcTokenStore(druidDataSource());
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthenticationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private MyUserDetailsService myUserDetailsService;
@Autowired
private TokenStore tokenStore;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("test-pc")
.secret(passwordEncoder.encode("123456"))
.resourceIds("oauth2-server")
.authorizedGrantTypes("authorization_code", "password", "implicit", "client_credentials", "refresh_token")
.scopes("all")
.autoApprove(false)
.redirectUris("http://www.baidu.com");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
endpoints.userDetailsService(myUserDetailsService);
//令牌管理策略
endpoints.tokenStore(tokenStore);
}
}
<dependency>
<groupId>org.springframework.securitygroupId>
<artifactId>spring-security-jwtartifactId>
<version>1.1.1.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-securityartifactId>
<version>2.3.12.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.security.oauthgroupId>
<artifactId>spring-security-oauth2artifactId>
<version>2.3.4.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<version>2.3.12.RELEASEversion>
dependency>
@Configuration
public class MyOAuth2Config {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public TokenStore tokenStore() {
// JWT令牌存储方式
return new JwtTokenStore(jwtAccessTokenConverter());
}
/**
* 帮助JWT编码的令牌值在OAuth身份验证信息之间进行转换
* JwtAccessTokenConverter是TokenEnhancer的一个实例
*/
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
// JWT签名的秘钥,这里使用的是对称加密,资源服务器使用该秘钥来验证
converter.setSigningKey("jwt");
return converter;
}
}
/**
* 当前需要使用内存方式存储了用户令牌,应当使用UserDetailsService才行,否则会报错
*/
@Component
public class MyUserDetailService implements UserDetailsService {
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
return new User("admin", passwordEncoder.encode("123456"),
AuthorityUtils.commaSeparatedStringToAuthorityList("admin_role"));
}
}
@EnableWebSecurity
public class OAuth2SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyUserDetailService myUserDetailService;
/**
* password密码模式要使用此认证管理器
*/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* 用户类信息
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myUserDetailService);
}
}
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private MyUserDetailService myUserDetailService;
@Autowired
private TokenStore tokenStore;
@Autowired
private JwtAccessTokenConverter jwtAccessTokenConverter;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("test-pc")
.secret(passwordEncoder.encode("123456"))
.resourceIds("oauth2-server")
.authorizedGrantTypes("password", "authorization_code", "refresh_token")
.scopes("all")
.autoApprove(false)
.redirectUris("http://www.baidu.com/");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//密码模式需要配置认证管理器
endpoints.authenticationManager(authenticationManager);
//刷新令牌获取新令牌时需要
endpoints.userDetailsService(myUserDetailService);
endpoints.tokenStore(tokenStore);
//配置JwtAccessToken转换器,将值转换为jwt
endpoints.accessTokenConverter(jwtAccessTokenConverter);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
//security.allowFormAuthenticationForClients();
//所有人都可访问/oauth/token_key,后面要获取公钥,默认拒绝访问
//注意:rsa时才有用,其他需要先认证才访问该接口
security.tokenKeyAccess("permitAll()");
//认证后可访问/oauth/check_token,默认拒绝访问
security.checkTokenAccess("permitAll()");
}
}
Token解析将得到PAYLOAD,如果想在JWT中添加额外信息,需要实现TokenEnhancer,相当于是一个Token增强器
@Configuration
public class MyOAuth2Config {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public TokenStore tokenStore() {
// JWT令牌存储方式
return new JwtTokenStore(jwtAccessTokenConverter());
}
/**
* 帮助JWT编码的令牌值在OAuth身份验证信息之间进行转换
* JwtAccessTokenConverter是TokenEnhancer的一个实例
*/
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
// JWT签名的秘钥,这里使用的是对称加密,资源服务器使用该秘钥来验证
converter.setSigningKey("jwt");
return converter;
}
}
/**
* token内容增强器
*/
@Component
public class JwtTokenEnhancer implements TokenEnhancer {
@Override
public OAuth2AccessToken enhance(OAuth2AccessToken oAuth2AccessToken, OAuth2Authentication oAuth2Authentication) {
Map<String, Object> info = new HashMap<>();
//为原有的token的载荷增加一些内容
//在对token进行解密时就可以拿到这里添加的信息
info.put("enhance", "enhance info");
((DefaultOAuth2AccessToken) oAuth2AccessToken).setAdditionalInformation(info);
return oAuth2AccessToken;
}
}
@EnableWebSecurity
public class OAuth2SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private MyUserDetailService myUserDetailService;
/**
* password密码模式要使用此认证管理器
*/
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
/**
* 用户类信息
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(myUserDetailService);
}
}
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private MyUserDetailService myUserDetailService;
@Autowired
private TokenStore tokenStore;
@Autowired
private JwtAccessTokenConverter jwtAccessTokenConverter;
@Autowired
private JwtTokenEnhancer jwtTokenEnhancer;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("test-pc")
.secret(passwordEncoder.encode("123456"))
.resourceIds("oauth2-server")
.authorizedGrantTypes("password", "authorization_code", "refresh_token")
.scopes("all")
.autoApprove(false)
.redirectUris("http://www.baidu.com/");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//配置JWT的内容增强器,TokenEnhancer可以对token进行增强
TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
List<TokenEnhancer> delegates = new ArrayList<>();
//添加token增强器
delegates.add(jwtTokenEnhancer);
//添加转换器
delegates.add(jwtAccessTokenConverter);
//把增强内容放入增强链中
enhancerChain.setTokenEnhancers(delegates);
//密码模式需要配置认证管理器
endpoints.authenticationManager(authenticationManager);
//刷新令牌获取新令牌时需要
endpoints.userDetailsService(myUserDetailService);
endpoints.tokenStore(tokenStore);
//配置JwtAccessToken转换器,将值转换为jwt
endpoints.accessTokenConverter(jwtAccessTokenConverter);
//配置token增强链
endpoints.tokenEnhancer(enhancerChain);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
//所有人都可访问/oauth/token_key,后面要获取公钥,默认拒绝访问
security.tokenKeyAccess("permitAll()");
//认证后可访问/oauth/check_token,默认拒绝访问
security.checkTokenAccess("permitAll()");
}
}
@Configuration
public class MyOAuth2Config {
@Autowired
private JwtTokenEnhancer jwtTokenEnhancer;
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
public TokenStore tokenStore(){
return new JwtTokenStore(jwtAccessTokenConverter());
}
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter(){
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey("123");
return converter;
}
/**
* 令牌管理服务
*/
@Bean
public AuthorizationServerTokenServices authorizationServerTokenServices(){
DefaultTokenServices tokenServices = new DefaultTokenServices();
// 客户端详情,因为是向客户端颁发令牌,所以需要知道是哪一个客户端
/*tokenServices.setClientDetailsService();*/
// 是否支持刷新令牌
tokenServices.setSupportRefreshToken(true);
// 令牌存储策略
tokenServices.setTokenStore(tokenStore());
// 设置令牌增强
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
List<TokenEnhancer> delegates = new ArrayList<>();
delegates.add(jwtTokenEnhancer);
delegates.add(jwtAccessTokenConverter());
tokenEnhancerChain.setTokenEnhancers(delegates);
tokenServices.setTokenEnhancer(tokenEnhancerChain);
// access_token默认有效期2小时
tokenServices.setAccessTokenValiditySeconds(7200);
// refresh_token默认有效期3天
tokenServices.setRefreshTokenValiditySeconds(259200);
return tokenServices;
}
}
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private MyUserDetailService myUserDetailService;
@Autowired
private AuthorizationServerTokenServices authorizationServerTokenServices;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("test-pc")
.secret(passwordEncoder.encode("123456"))
.resourceIds("oauth2-server")
.authorizedGrantTypes("password", "authorization_code", "refresh_token")
.scopes("all")
.autoApprove(false)
.redirectUris("http://www.baidu.com/");
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
//密码模式需要配置认证管理器
endpoints.authenticationManager(authenticationManager);
//刷新令牌获取新令牌时需要
endpoints.userDetailsService(myUserDetailService);
//令牌管理服务
endpoints.tokenServices(authorizationServerTokenServices);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
//所有人都可访问/oauth/token_key,后面要获取公钥,默认拒绝访问
security.tokenKeyAccess("permitAll()");
//认证后可访问/oauth/check_token,默认拒绝访问
security.checkTokenAccess("permitAll()");
}
}
默认情况下/oauth/check_token和/oauth/token_key端点默认是denyAll()拒绝访问的权限,如果这两个端点需要访问,要对他们进行认证和授权才可以访问
@Configuration
@EnableAuthorizationServer
public class OAuth2AuthenticationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private MyUserDetailsService myUserDetailsService;
@Autowired
private TokenStore tokenStore;
@Autowired
private AuthorizationCodeServices jdbcAuthorizationCodeServices;
@Autowired
private ClientDetailsService jdbcClientDetailsService;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(jdbcClientDetailsService);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
endpoints.userDetailsService(myUserDetailsService);
//令牌管理策略
endpoints.tokenStore(tokenStore);
//授权码管理策略,针对授权码模式有效,会将授权码放到oauth_code表,授权后就删除它
endpoints.authorizationCodeServices(jdbcAuthorizationCodeServices);
}
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
//所有人都可访问/oauth/token_key,后面要获取rsa公钥,默认拒绝访问
//注意:rsa时才有用,其他需要先认证才访问该接口
security.tokenKeyAccess("permitAll()");
//认证后可访问/oauth/check_token,默认拒绝访问
security.checkTokenAccess("isAuthenticated()");
}
}