上一篇博客使用自定義jwt實現spring cloud nosession,過程稍微比較複雜,依賴的是咱們本身控制token生成、校驗。 那麼這一篇文章將使用spring cloud 和 spring-security-oauth2 作一個無縫集成,實現nosession,和權限控制。git
爲了快速的實現目標效果,拓撲結構如上圖,咱們將採用 InMemory的形式實現,相關換成JDBC實現位置,我會在文章中說明,主要看代碼的註釋。redis
認證明現spring
@Configuration @EnableAuthorizationServer class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter { @Autowired private AuthenticationManager authenticationManager; @Autowired private UserDetailsService userDetailsService; @Override public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception { endpoints.authenticationManager(authenticationManager).tokenStore(tokenStore()).userDetailsService(userDetailsService); } @Override public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception { oauthServer.tokenKeyAccess("permitAll()"); } //能夠改爲JDBC從庫裏讀或者其餘方式。 @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("browser") .authorizedGrantTypes("authorization_code", "refresh_token", "password") .scopes("ui") .and() .withClient("resource-server") .secret("root") .authorizedGrantTypes("client_credentials", "refresh_token") .scopes("server"); } //這裏配置的配置能夠改爲 jdbc redis 等其餘方式 @Bean public InMemoryTokenStore tokenStore() { return new InMemoryTokenStore(); } } // security配置主要是userDetailsService @Configuration @EnableWebSecurity class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService userDetailsService; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth.userDetailsService(userDetailsService); } @Override protected void configure(HttpSecurity http) throws Exception { http.anonymous().disable() .authorizeRequests() .anyRequest().authenticated(); } }
關於爲何要配置loadBalancerInterceptor這個bean 我會在這篇文章總結部分說明chrome
@SpringBootApplication @EnableEurekaClient @EnableResourceServer public class ResourceApplication { public static void main(String[] args) { SpringApplication.run(ResourceApplication.class, args); } @Bean LoadBalancerInterceptor loadBalancerInterceptor(LoadBalancerClient loadBalance) { return new LoadBalancerInterceptor(loadBalance); } }
還有一個最重要的,就是咱們受保護的資源。。session
@RestController public class ResourceController { @GetMapping("/getResource") public String getResource(Principal principal) { return "SUCCESS,受權成功拿到資源啦.當前用戶:" + principal.getName(); } }
配置application.ymlapp
security: sessions: stateless oauth2: resource: loadBalanced: true user-info-uri: http://gateway-server/uaa/user prefer-token-info: false service-id: resource-server
zuul: ignored-services: '*' routes: auth-server: path: /uaa/** serviceId: auth-server sensitiveHeaders: strip-prefix: false resource-server: path: /demo/** serviceId: resource-server sensitiveHeaders: strip-prefix: false
服務 | 端口 |
---|---|
gateway-server | 6666 |
auth-server | 7777 |
resource-server | 8888 |
eureka-server | http://eureka.didispace.com/eureka/ |
//curl 不會用的能夠參考文章總結裏面 Rest Client 那個報文工具。 // 這裏的帳戶是在 auth-server裏面寫死的,service實現裏面。 curl -H "Authorization:Basic YnJvd3Nlcjo=" -d "grant_type=password&scope=ui&username=123&password=456" localhost:6666/uaa/oauth/token {"access_token":"c8df8942-f032-4403-92fc-f23019819da9","token_type":"bearer","refresh_token":"993a94b4-5335-4f0a-9981-e1ad4e4776a8","expires_in":43199,"scope":"ui"}
curl -H "Authorization:Bearer c8df8942-f032-4403-92fc-f23019819da9" localhost:6666/demo/getResource SUCCESS,受權成功拿到資源啦.當前用戶:123
security: sessions: stateless oauth2: resource: loadBalanced: true #設置這個才能夠使用eureka的服務名,配合loadBalancerInterceptor bean。
3.關於CURL 工具 能夠用 Git Bash ,提供了curl 工具的。或者chrome裏面這個插件挺好用。 less
springcloud-security-oauth-server
springcloud-security-oauth-resource
springcloud-security-oauth-gatewaycurl
這個三個模塊ide