SpringSecurity+Oauth2+JWT令牌加密token

水深无声 2022-12-21 09:23 215阅读 0赞

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

#

18f92d1709d0b777194eab45c8e5d28c.png

目录结构:
ee0f1928a999c9969b1cbe2aea60a40d.png
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. springcloud1
  6. org.example
  7. 1.0-SNAPSHOT
  8. 4.0.0
  9. springcloud-oauth-order-8004
  10. org.springframework.cloud
  11. spring-cloud-dependencies
  12. Finchley.RELEASE
  13. pom
  14. import
  15. org.springframework.boot
  16. spring-boot-starter-web
  17. 2.1.4.RELEASE
  18. org.springframework.boot
  19. spring-boot-test
  20. org.springframework.cloud
  21. spring-cloud-netflix-eureka-client
  22. org.springframework.boot
  23. spring-boot-starter-security
  24. org.springframework.cloud
  25. spring-cloud-starter-oauth2
  26. org.springframework.security
  27. spring-security-jwt

Mysecurityconfig配置:

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

resourceconfig配置:

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

Tokenconfig配置:

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

获取到的token:

d80b709b66be2a6428cc06f0da11dc1b.png

发表评论

表情:
评论列表 (有 0 条评论,215人围观)

还没有评论,来说两句吧...

相关阅读