springcloud gateway + oauth2 实战总结
作者:互联网
springcloud gateway + oauth2 实战总结
1,认证中心
引入依赖
<properties>
<oauth2.version>2.2.1.RELEASE</oauth2.version>
<nimbusds-jose-jwt.version>9.15.2</nimbusds-jose-jwt.version>
</properties>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
<version>${oauth2.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>${nimbusds-jose-jwt.version}</version>
</dependency>
生成jwt.jks公钥
keytool -genkey -alias jwt -keyalg RSA -keystore jwt.jks
keytool -importkeystore -srckeystore jwt.jks -destkeystore jwt.jks -deststoretype pkcs12
jwt.jks 放到resources文件夹下
authentication:
keyPath: jwt.jks
keyPassword: xxxxxx
keyPair: jwt
实现接口UserDetailsService重载loadUserByUsername获取用户信息
配置权限
继承AuthorizationServerConfigurerAdapter
@Configuration
@EnableAuthorizationServer
设置验证方式,配置保存方式,授权方式
权限校验
继承WebSecurityConfigurerAdapter
@Configuration
@EnableWebSecurity
设置用户权限拦截,用户信息,加密方式,权限管理
提供公钥访问接口
2,网关配置
引入依赖
<properties>
<nimbusds-jose-jwt.version>9.15.2</nimbusds-jose-jwt.version>
</properties>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-jose</artifactId>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>${nimbusds-jose-jwt.version}</version>
</dependency>
设置跨域
安全配置
@Configuration
@EnableWebFluxSecurity
实现认证管理器,用于权限判断
ReactiveAuthorizationManager<AuthorizationContext>
设置访问公钥地址
spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: 'http://localhost:8800/user/rsa/publicKey'
3,资源服务
继承ResourceServerConfigurerAdapter
设置token方式
标签:oauth2,springcloud,jwt,springframework,spring,org,security,gateway 来源: https://blog.csdn.net/u011089598/article/details/122390558