//受權服務器配置 @Configuration @EnableAuthorizationServer public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter { @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientapp") .secret("112233") .redirectUris("http://localhost:9001/callback") // 受權碼模式 .authorizedGrantTypes("authorization_code") .scopes("read_userinfo", "read_contacts"); } }
//資源服務配置 @Configuration @EnableResourceServer public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter { @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest() .authenticated() .and() .requestMatchers() .antMatchers("/api/**"); } }
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=code&scope=read_userinfo
# Spring Security Setting security.user.name=bobo security.user.password=xyz
http://localhost:8080/oauth/token?code=ghN0hF&grant_type=authorization_code&redirect_uri=http://localhost:9001/callback&scope=read_userinfo
注意:須要在headers裏添加認證
認證參數就是受權服務器配置的client和secrethtml
http://localhost:8080/api/userinfo?access_token=f4345f3a-34a3-4887-bc02-e95150c54bf4
若是token錯誤,則
git
@Configuration @EnableAuthorizationServer public class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{ @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientapp") .secret("112233") .accessTokenValiditySeconds(60) .redirectUris("http://localhost:9001/callback") .authorizedGrantTypes("implicit") .scopes("admin", "visitor"); } }
http://localhost:8080/oauth/authorize?client_id=clientapp&redirect_uri=http://localhost:9001/callback&response_type=token&scope=admin&state=abc
注意:由於Access token是附着在 redirect_uri 上面被返回的,因此這個 Access token就可能會暴露給資源全部者或者設置內的其它方(對資源全部者來講,能夠看到redirect_uri,對其它方來講,能夠經過監測瀏覽器的地址變化來獲得 Access token)。github
// 受權服務器配置 @Configuration @EnableAuthorizationServer public class OAuth2AuthoriationServer extends AuthorizationServerConfigurerAdapter{ @Autowired private AuthenticationManager authenticationManager; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager); } @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("clientapp") .secret("112233") .accessTokenValiditySeconds(60) .redirectUris("http://localhost:9001/callback") .authorizedGrantTypes("password") .scopes("admin", "visitor"); } }
http://localhost:8080/oauth/token?password=123456&grant_type=password&username=lll&scope=admin
注意:和受權碼模式同樣,須要在headers裏添加認證spring
結果:
api
獲取token後,步驟同1.1和1.2模式瀏覽器
http://localhost:8080/oauth/token?grant_type=client_credentials&scope=admin
http://localhost:8080/oauth/token?grant_type=refresh_token&refresh_token=ad3941d1-c6dd-4a2e-a9c8-eac6a9a59dd2
參考 https://www.cnblogs.com/maoxiaolv/p/5838680.html安全
代碼學習地址 https://github.com/spring2go/oauth2lab服務器