Spring Boot整合Spring Security简记-O
2018-03-16 本文已影响684人
78240024406c
new無语 转载请注明原创出处,谢谢!
上接Spring Boot整合Spring Security简记-OAuth2(十一)
过滤器
-
ClientCredentialsTokenEndpointFilter: 请求/oauth/token认证客户端。
相关类
-
AuthorizationServerTokenServices:令牌管理。 -
AuthorizationCodeServices设置为JdbcAuthorizationCodeServices,使用jdbc进行维护code,将code存储在表oauth_code(框架自带的表结构,在文末会一起给出)。 -
TokenGranter: 接入令牌的接口,提供令牌操作扩展(grant_type参数authorization_code,password,implicit操作等)。
请求端点
-
AuthorizationEndpoint用于为授权请求提供服务。默认网址:/oauth/authorize。 -
TokenEndpoint用于服务访问令牌的请求。默认网址:/oauth/token。
InMemory 基于内存存储令牌
添加客户端信息
@Configuration
@EnableAuthorizationServer
public class OAuth2Configurer extends AuthorizationServerConfigurerAdapter {
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
//添加客户端信息
// 使用in-memory存储客户端信息
clients.inMemory()
// client_id
.withClient("client")
// client_secret
.secret("secret")
// 该client允许的授权类型
.authorizedGrantTypes("authorization_code")
// 允许的授权范围
.scopes("app");
}
}
设置basic登陆
@Configuration
@EnableWebSecurity
public class WebSecurityConfigurer extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic();
}
}
之后请求端点地址,就可以访问授权。
GET response_type设置为code,获取code后,请求/oauth/token获取access_token。(authorized_grant_types设置为authorization_code)
http://localhost:8080/oauth/authorize?client_id=client&response_type=code&redirect_uri=http://www.jianshu.com
GET response_type设置为token直接获取access_token。(authorized_grant_types设置为implicit)
http://localhost:8080/oauth/authorize?client_id=client&response_type=token&redirect_uri=http://www.jianshu.com
之后会请求登陆,默认登录名user,密码在控制台输出了。复制出来就OK了。
获取密码
登陆
跳转到很low的授权页面,点击按钮,就会授权获取
code跳转到简书页面。
默认授权页面
授权成功回调页面
POST通过code获取access_token
http://client:secret@localhost:8080/oauth/token
参数
Content-Type:application/x-www-form-urlencoded
code:psvucZ
grant_type:authorization_code
redirect_uri:http://www.jianshu.com
POM依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
</dependency>
JDBC 基于JDBC存储令牌
设置客户端信息存储方式为jdbc:
@Autowired
private DataSource dataSource;
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security
.allowFormAuthenticationForClients()
.passwordEncoder(new BCryptPasswordEncoder())
.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
//添加客户端信息
clients.jdbc(dataSource);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authorizationCodeServices(new JdbcAuthorizationCodeServices(dataSource))
.authenticationManager(authenticationManager);
}
-
.authenticationManager(authenticationManager),否则不会启动账号/密码获取token方式。源码如下
AuthorizationServerEndpointsConfigurer部分源码:
if (authenticationManager != null) {
tokenGranters.add(new ResourceOwnerPasswordTokenGranter(authenticationManager, tokenServices,
clientDetails, requestFactory));
}
-
security.allowFormAuthenticationForClients()开启过滤器认证客户端信息。进行token令牌操作前置。(账号/密码认证等) -
.tokenKeyAccess("permitAll()")开启/oauth/token_key验证端口无权限访问。 -
.checkTokenAccess("isAuthenticated()")开启/oauth/check_token验证端口认证权限访问。
POST http://localhost:8080/oauth/token 通过账户/密码获取access_token。
参数
Content-Type:application/x-www-form-urlencoded
password:password
username:user
client_id:client
client_secret:secret
grant_type:password
密码授权认证