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了。
![](https://img.haomeiwen.com/i2596637/bce919423250181f.png)
![](http://upload-images.jianshu.io/upload_images/2596637-14fd230f5bcdabc3.png)
跳转到很low的授权页面,点击按钮,就会授权获取
code
跳转到简书页面。![](http://upload-images.jianshu.io/upload_images/2596637-4f32c7e3b0a8c2b3.png)
![](http://upload-images.jianshu.io/upload_images/2596637-9db00da554f74f81.png)
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
![](http://upload-images.jianshu.io/upload_images/2596637-1a8f5b4a373c0f76.png)