🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 添加依赖 ```xml <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency> ``` 完整版 ```xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloudframe</artifactId> <groupId>com.gosuncn</groupId> <version>1.0</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>sys-provider</artifactId> <dependencies> <dependency> <groupId>com.gosuncn</groupId> <artifactId>common</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency> </dependencies> <build> <resources> <resource> <directory>src/main/java</directory> <includes> <include>com/gosuncn/dao/*.xml</include> </includes> </resource> </resources> </build> </project> ``` # 配置PasswordEncoder密码编码器 ```java package com.gosuncn.config; @Configuration public class PasswordEncoderConfig { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(); } } ``` # 配置令牌存储策略 ```java package com.gosuncn.config; @Configuration public class TokenStoreConfig { private static final String DIR_NAME = "PublicKey" + "/"; @Value("${cloudframe.public-key}") private String publicKey; @Bean public JwtAccessTokenConverter accessTokenConverter() { JwtAccessTokenConverter converter = new JwtAccessTokenConverter(); try { converter.setVerifierKey(new String(FileCopyUtils.copyToByteArray(new ClassPathResource(DIR_NAME + publicKey).getInputStream()))); } catch (IOException e) { e.printStackTrace(); } return converter; } @Bean public TokenStore tokenStore() { return new JwtTokenStore(accessTokenConverter()); } } ``` # 配置文件 - 新增配置 ```yaml cloudframe: public-key: public.cert ``` # 配置资源服务器 ```java package com.gosuncn.config; @Configuration @AllArgsConstructor @EnableResourceServer public class ResourceServerConfig extends ResourceServerConfigurerAdapter { private static final String RESOURCE_ID = "provider"; private final TokenStore tokenStore; @Override public void configure(ResourceServerSecurityConfigurer resources) { resources.resourceId(RESOURCE_ID) .tokenStore(tokenStore) .stateless(true); } @Override public void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/**").access("#oauth2.hasScope('all')") .and() .csrf().disable() .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); } } ``` # 添加公钥 在`resources`下新建`PublicKey`文件夹,将`public.cert`文件放入。