SpringBoot2.7.2整合SpringSecurity+Redis权限控制
时间: 2023-08-24 15:14:31 浏览: 123
jedis-jedis-2.7.2 (1).tar.gz
好的,我可以为您提供一些指导。
首先,您需要在您的Spring Boot项目中添加Spring Security和Redis的依赖。
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
```
然后,在您的Spring Boot项目中创建一个配置类,该类将配置Spring Security和Redis的集成。
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.logout()
.logoutSuccessUrl("/");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
RedisUserDetailsService userDetailsService = new RedisUserDetailsService(redisConnectionFactory);
auth.userDetailsService(userDetailsService);
}
@Bean
public RedisTokenRepositoryImpl redisTokenRepository() {
return new RedisTokenRepositoryImpl(redisConnectionFactory);
}
@Bean
public TokenBasedRememberMeServices rememberMeServices() {
TokenBasedRememberMeServices rememberMeServices = new TokenBasedRememberMeServices("remember-me", userDetailsService());
rememberMeServices.setTokenRepository(redisTokenRepository());
return rememberMeServices;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
```
上述配置类中的configure(HttpSecurity http)方法指定了不同角色的用户可以访问哪些URL。configure(AuthenticationManagerBuilder auth)方法指定了如何从Redis中获取用户信息。redisTokenRepository()和rememberMeServices()方法指定了如何将Remember-Me令牌存储到Redis中。
最后,在您的Spring Boot项目中创建一个RedisUserDetailsService类,该类将从Redis中获取用户信息。
```java
public class RedisUserDetailsService implements UserDetailsService {
private RedisConnectionFactory redisConnectionFactory;
public RedisUserDetailsService(RedisConnectionFactory redisConnectionFactory) {
this.redisConnectionFactory = redisConnectionFactory;
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericToStringSerializer<>(Object.class));
redisTemplate.afterPropertiesSet();
Map<Object, Object> userMap = redisTemplate.opsForHash().entries("user:" + username);
if (userMap.isEmpty()) {
throw new UsernameNotFoundException("User '" + username + "' not found");
}
String password = (String) userMap.get("password");
List<String> authorities = (List<String>) userMap.get("authorities");
List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
for (String authority : authorities) {
grantedAuthorities.add(new SimpleGrantedAuthority(authority));
}
return new User(username, password, grantedAuthorities);
}
}
```
上述类中的loadUserByUsername(String username)方法从Redis中获取用户信息。
希望这些信息能对您有所帮助!
阅读全文