github仓库地址:https://github.com/Sjj1024/SpringCloudDemo

 

SpringSecurity+Oauth2+JWT令牌加密token

目录结构:
SpringSecurity+Oauth2+JWT令牌加密token
pom文件:

 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <parent>
  6. <artifactId>springcloud1</artifactId>
  7. <groupId>org.example</groupId>
  8. <version>1.0-SNAPSHOT</version>
  9. </parent>
  10. <modelVersion>4.0.0</modelVersion>
  11.  
  12. <artifactId>springcloud-oauth-order-8004</artifactId>
  13.  
  14. <dependencies>
  15. <dependency>
  16. <groupId>org.springframework.cloud</groupId>
  17. <artifactId>spring-cloud-dependencies</artifactId>
  18. <version>Finchley.RELEASE</version>
  19. <type>pom</type>
  20. <scope>import</scope>
  21. </dependency>
  22.  
  23. <dependency>
  24. <groupId>org.springframework.boot</groupId>
  25. <artifactId>spring-boot-starter-web</artifactId>
  26. <version>2.1.4.RELEASE</version>
  27. </dependency>
  28.  
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-test</artifactId>
  32. </dependency>
  33.  
  34. <dependency>
  35. <groupId>org.springframework.cloud</groupId>
  36. <artifactId>spring-cloud-netflix-eureka-client</artifactId>
  37. </dependency>
  38.  
  39. <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-security -->
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-security</artifactId>
  43. </dependency>
  44.  
  45. <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-oauth2 -->
  46. <dependency>
  47. <groupId>org.springframework.cloud</groupId>
  48. <artifactId>spring-cloud-starter-oauth2</artifactId>
  49. </dependency>
  50.  
  51. <!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-jwt -->
  52. <dependency>
  53. <groupId>org.springframework.security</groupId>
  54. <artifactId>spring-security-jwt</artifactId>
  55. </dependency>
  56. </dependencies>
  57.  
  58.  
  59. </project>

 

Mysecurityconfig配置:

 
  1. package com.shen.config;
  2.  
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  5. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  6.  
  7. @Configuration
  8. public class MySecurityConfg extends WebSecurityConfigurerAdapter {
  9.  
  10. @Override
  11. protected void configure(HttpSecurity http) throws Exception {
  12. // 决定那些请求被拦截
  13. http
  14. .csrf().disable()
  15. .authorizeRequests()
  16. .antMatchers("/css/**", "/js/**", "/fonts/**", "/index").permitAll() //都可以访问
  17. .antMatchers("/order/**").authenticated() // 所有order下的请求都要认证
  18. .anyRequest().permitAll(); // 其他请求都可以访问
  19. // .and()
  20. // .formLogin()
  21. // .loginProcessingUrl("/login")
  22. // .permitAll()// 表单登录允许任意权限访问
  23. // .and()
  24. // .logout().permitAll();// 注销操作允许任意权限访问
  25. }
  26. }
  27.  

 

resourceconfig配置:

 
  1. package com.shen.config;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  7. import org.springframework.security.config.http.SessionCreationPolicy;
  8. import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
  9. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurer;
  10. import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
  11. import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
  12. import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
  13. import org.springframework.security.oauth2.provider.token.ResourceServerTokenServices;
  14. import org.springframework.security.oauth2.provider.token.TokenStore;
  15.  
  16. import java.rmi.Remote;
  17.  
  18. @Configuration
  19. @EnableResourceServer
  20. public class ResouceServeCongie extends ResourceServerConfigurerAdapter {
  21.  
  22. // 配置资源ID
  23. public static final String RESOURCE_ID = "resource1";
  24.  
  25. // // 配置令牌验证的服务
  26. // @Bean
  27. // public ResourceServerTokenServices tokenServices(){
  28. // RemoteTokenServices services = new RemoteTokenServices();
  29. // services.setCheckTokenEndpointUrl("http://localhost:8003/oauth/check_token");
  30. // services.setClientId("client1");
  31. // services.setClientSecret("secret");
  32. // return services;
  33. // }
  34.  
  35. // 注入本地验证的配置类
  36. @Autowired
  37. TokenStore tokenStore;
  38.  
  39. // 配置资源服务
  40. @Override
  41. public void configure(ResourceServerSecurityConfigurer resources){
  42. resources.resourceId(RESOURCE_ID)
  43. // .tokenServices(tokenServices()) // 验证令牌的服务
  44. .tokenStore(tokenStore) // 使用远程校验令牌的服务
  45. .stateless(true);
  46. }
  47.  
  48. // 配置HTTP服务
  49. public void configure(HttpSecurity http) throws Exception {
  50. http.
  51. authorizeRequests()
  52. .antMatchers("/**").access("#oauth2.hasScope('all')")
  53. .and().csrf().disable()
  54. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
  55. }
  56.  
  57. }
  58.  

 

Tokenconfig配置:

 
  1. package com.shen.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.security.oauth2.provider.token.TokenStore;
  6. import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
  7. import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
  8.  
  9. @Configuration
  10. public class TokenConfig {
  11.  
  12. // 使用本地校验token的方式,所以需要配置单独的令牌校验服务
  13. // 配置JWT令牌的相关配置
  14.  
  15. // 配置**
  16. private String SIGNING_KEY = "uaa_authorization";
  17.  
  18. // 配置JWT存储方案
  19. @Bean
  20. public TokenStore tokenStore(){
  21. return new JwtTokenStore(accessTokenConverter());
  22. }
  23.  
  24. // 配置生成JWT令牌的过程
  25. @Bean
  26. public JwtAccessTokenConverter accessTokenConverter(){
  27. JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
  28. converter.setSigningKey(SIGNING_KEY);
  29. return converter;
  30. }
  31.  
  32. }
  33.  

 

获取到的token:

SpringSecurity+Oauth2+JWT令牌加密token

相关文章: