AuthorizationServer:授权服务配置
一、授权服务配置类:AuthorizationServerConfigurerAdapter
– SpringSecurityOauth2提供了AuthorizationServerConfigurerAdapter适配器类来作为认证授权服务的配置,其中有三个方法,
– 客户端详情:配置客户端请求的参数;
– 授权服务断点:配置授权码和令牌的管理/存储方式;
– 授权服务安全配置:配置哪些路径放行(检查token的路径要放行);
二、搭建auth2授权服务配置
1.编写一个配置类: 继承AuthorizationServerConfigurerAdapter,
开启认证配置服务标签:@EnableAuthorizationServer;
2.复写三个配置
2.1 客户端详情配置:数据库创建一张表:auth_client_details 插入两条数据;
定义一个JdbcClientDetailsService方法,需要两个参数:datasource,passwordEncoder;
2.2 服务端点配置:
①:配置认证管理器:在Security配置类中配置authenticationManager
②:授权服务配置器:a:数据库创建一张表 auth_code
b:定义方法 JdbcAuthorizationCodeServices 需要一个参数datasource;
③:令牌服务配置:new DefaultTokenService()
a: 设置客户端详情,
b: 设置token刷新,
c:设置TokenStore
d:Token 加强 ,令牌转换器
④:允许post方式请求;
2.3 服务安全配置 ;check_token放行
AuthorizationServerConfig:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.code.JdbcAuthorizationCodeServices;
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import javax.sql.DataSource;
import java.util.Arrays;
//授权服务配置
@Configuration
//开启授权服务配置
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
//1.客户端详情配置(请求参数)
@Autowired
private DataSource dataSource ;
@Autowired
private PasswordEncoder passwordEncoder ;
//1.1.注册客户端详情Bean,基于数据库,自动操作表:oauth_client_details
@Bean
public JdbcClientDetailsService jdbcClientDetailsService(){
JdbcClientDetailsService jdbcClientDetailsService = new JdbcClientDetailsService(dataSource);
//数据库的秘钥使用了PasswordEncoder加密
jdbcClientDetailsService.setPasswordEncoder(passwordEncoder);
return jdbcClientDetailsService;
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.withClientDetails(jdbcClientDetailsService());
}
//2.授权服务端点配置(授权码,令牌)
@Autowired
private AuthenticationManager authenticationManager ;
//2.1.定义授权码服务,连接数据库 oauth_code
@Bean
public JdbcAuthorizationCodeServices jdbcAuthorizationCodeServices(){
return new JdbcAuthorizationCodeServices(dataSource);
}
//2.2.令牌服务配置
//令牌的管理服务
@Bean
public AuthorizationServerTokenServices tokenService(){
//创建默认的令牌服务
DefaultTokenServices services = new DefaultTokenServices();
//指定客户端详情配置
services.setClientDetailsService(jdbcClientDetailsService());
//支持产生刷新token
services.setSupportRefreshToken(true);
//token存储方式
services.setTokenStore(tokenStore());
//设置token增强 - 设置token转换器
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
tokenEnhancerChain.setTokenEnhancers(Arrays.asList(jwtAccessTokenConverter()));
services.setTokenEnhancer(tokenEnhancerChain); //jwtAccessTokenConverter()
return services;
}
//2.3.配置Token的存储方案
//基于内存的Token存储
@Bean
public TokenStore tokenStore(){
//return new InMemoryTokenStore();
return new JwtTokenStore(jwtAccessTokenConverter());
}
//2.4.配置令牌转换器 ,设置JWT签名密钥。它可以是简单的MAC密钥,也可以是RSA密钥
private final String sign_key = "123";
//JWT令牌校验工具
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter(){
JwtAccessTokenConverter jwtAccessTokenConverter = new JwtAccessTokenConverter();
//设置JWT签名密钥。它可以是简单的MAC密钥,也可以是RSA密钥
jwtAccessTokenConverter.setSigningKey(sign_key);
return jwtAccessTokenConverter;
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints
//1.密码授权模式需要 密码模式,需要认证管理器
.authenticationManager(authenticationManager)
//2.授权码模式服务
.authorizationCodeServices(jdbcAuthorizationCodeServices())
//3.配置令牌管理服务
.tokenServices(tokenService())
//允许post方式请求
.allowedTokenEndpointRequestMethods(HttpMethod.POST);
}
//2.授权服务安全配置(URL放行)
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
//对应/oauth/check_token ,路径公开
.checkTokenAccess("permitAll()")
//允许客户端进行表单身份验证,使用表单认证申请令牌
.allowFormAuthenticationForClients();
}
}
SecurityConfig:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
//密码加密器
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
//授权规则配置
@Override
protected void configure(HttpSecurity http) throws Exception {
//授权配置
http.csrf().disable() //屏蔽跨域防护
.authorizeRequests() //对请求做授权处理
.antMatchers("/login").permitAll() //登录路径放行
// .antMatchers("/login.html").permitAll()//对登录页面跳转路径放行
.anyRequest().authenticated() //其他路径都要拦截
.and().formLogin() //允许表单登录, 设置登陆页
.successForwardUrl("/loginSuccess") // 设置登陆成功页(对应//controller),一定要有loginSuccess这个路径
.and().logout().permitAll(); //登出
}
//配置认证管理器,授权模式为“poassword”时会用到
@Bean
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
}