spring boot security jwt
时间: 2023-10-07 08:10:20 浏览: 60
Spring Boot Security与JWT(JSON Web Token)的整合是一种常见的身份验证和授权机制。通过引入Spring Security和Jwt相关依赖,可以轻松地实现基于Token的用户身份验证和授权。
整合Jwt的配置一般包括以下几个方面:
1. 引入Spring Security依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>。
2. 配置登录拦截器和验证拦截器:可以自定义LoginFilter和AuthenticationFilter来处理登录和验证的逻辑。
3. 自定义异常处理:可以实现RestAuthenticationAccessDeniedHandler和AuthEntryPoint来处理权限不足和令牌失效的情况。
4. 接口鉴权和忽略认证:可以使用Spring Security提供的注解和配置来控制接口的访问权限和忽略认证。
通过整合Spring Boot Security和Jwt,可以实现系统的权限控制,并支持各种粒度的权限控制。同时,Spring Security还提供了丰富的自定义处理器和拦截器等功能,可以灵活地满足不同的业务需求。
相关问题
spring boot Security jwt aouth2整合代码
以下是Spring Boot集成Spring Security、JWT和OAuth2的示例代码:
1. 添加依赖
在pom.xml文件中添加以下依赖:
```xml
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Spring Security OAuth2 -->
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
<!-- JWT -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
```
2. 配置Spring Security
在Spring Boot应用程序中,可以通过配置类来配置Spring Security。创建一个配置类,命名为SecurityConfig.java,并添加以下内容:
```java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests().antMatchers("/oauth/**").permitAll()
.anyRequest().authenticated()
.and().formLogin().permitAll();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}
```
这个类配置了Spring Security,启用了Web安全性,并定义了一个UserDetailsService实例,该实例将用于验证用户。
3. 配置OAuth2
创建一个OAuth2配置类,命名为OAuth2Config.java,并添加以下内容:
```java
@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private DataSource dataSource;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.jdbc(dataSource);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService)
.accessTokenConverter(accessTokenConverter());
}
@Bean
public TokenStore tokenStore() {
return new JdbcTokenStore(dataSource);
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey("secret");
return converter;
}
@Override
public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
}
@Bean
public ClientDetailsService clientDetailsService() {
return new JdbcClientDetailsService(dataSource);
}
@Bean
public ApprovalStore approvalStore() {
return new JdbcApprovalStore(dataSource);
}
}
```
这个类配置了OAuth2服务端,启用了授权服务器,并定义了一个客户端详情服务实例,该实例将用于管理客户端的详细信息。它还定义了一个令牌存储实例,该实例将用于存储访问令牌。
4. 配置JWT
创建一个JWT配置类,命名为JwtConfig.java,并添加以下内容:
```java
@Configuration
public class JwtConfig {
@Value("${jwt.secret}")
private String secret;
@Bean
public Key key() {
return Keys.hmacShaKeyFor(secret.getBytes());
}
@Bean
public JwtDecoder jwtDecoder() {
return NimbusJwtDecoder.withSecretKey(key()).build();
}
@Bean
public JwtEncoder jwtEncoder() {
return NimbusJwtEncoder.withSecretKey(key()).build();
}
}
```
这个类配置了JWT,定义了一个秘钥,该秘钥将用于签署和验证JWT令牌。
5. 配置资源服务器
创建一个资源服务器配置类,命名为ResourceServerConfig.java,并添加以下内容:
```java
@Configuration
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
@Autowired
private JwtAccessTokenConverter accessTokenConverter;
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/**").authenticated()
.anyRequest().permitAll();
}
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.tokenStore(tokenStore()).accessTokenConverter(accessTokenConverter);
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter);
}
}
```
这个类配置了资源服务器,启用了资源服务器,并定义了一个令牌存储实例,该实例将用于存储访问令牌。
6. 创建实体类
创建一个User实体类,命名为User.java,并添加以下内容:
```java
@Entity
@Table(name = "users")
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(unique = true)
private String username;
private String password;
@ElementCollection(fetch = FetchType.EAGER)
private List<String> roles;
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return roles.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
}
@Override
public String getPassword() {
return password;
}
@Override
public String getUsername() {
return username;
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
```
这个类定义了一个用户实体,实现了UserDetails接口,该接口提供了有关用户的详细信息。
7. 创建数据访问对象
创建一个UserRepository接口,命名为UserRepository.java,并继承JpaRepository接口,添加以下内容:
```java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByUsername(String username);
}
```
这个接口定义了一个用户数据访问对象,用于管理用户。
8. 实现用户服务
创建一个UserServiceImpl类,命名为UserServiceImpl.java,并添加以下内容:
```java
@Service
public class UserServiceImpl implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
return userRepository.findByUsername(username)
.orElseThrow(() -> new UsernameNotFoundException("User not found"));
}
}
```
这个类实现了UserDetailsService接口,该接口提供了一个方法,根据用户名加载用户信息。
9. 创建控制器
创建一个UserController类,命名为UserController.java,并添加以下内容:
```java
@RestController
@RequestMapping("/api")
public class UserController {
@GetMapping("/users")
public List<User> getUsers() {
return userRepository.findAll();
}
@PostMapping("/users")
public User createUser(@RequestBody User user) {
user.setPassword(passwordEncoder.encode(user.getPassword()));
return userRepository.save(user);
}
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
}
```
这个类定义了一个RESTful API控制器,用于管理用户。
10. 测试
启动应用程序,并使用Postman测试API接口。例如,使用POST方法向/api/users端点发送请求,创建一个新用户。
这就是Spring Boot集成Spring Security、JWT和OAuth2的示例代码。
Spring Boot Security OAuth2 JWT代码
以下是一个基于Spring Boot Security、OAuth2和JWT实现的示例代码:
1. 添加Maven依赖
在pom.xml文件中添加以下依赖:
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
```
2. 配置Spring Security
在Spring Boot应用程序中,您可以通过@Configuration配置类来配置Spring Security。以下是一个示例配置类:
```
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private PasswordEncoder passwordEncoder;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/oauth/**").permitAll()
.antMatchers("/api/**").authenticated()
.and()
.formLogin().permitAll()
.and()
.logout().permitAll();
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder);
}
}
```
在上面的配置类中,我们禁用了CSRF保护,并配置了访问权限。具体来说:
- /oauth/** URL模式应该允许所有人访问,因为我们将使用OAuth2协议进行身份验证和授权。
- /api/** URL模式应该需要身份验证。
- 我们还配置了表单登录和注销。
3. 配置OAuth2
在Spring Boot应用程序中,您可以使用@Configuration配置类来配置OAuth2。以下是一个示例配置类:
```
@Configuration
@EnableAuthorizationServer
public class OAuth2Config extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
private PasswordEncoder passwordEncoder;
@Bean
public JwtAccessTokenConverter jwtAccessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey("my-signing-key"); // 设置JWT签名密钥
return converter;
}
@Bean
public JwtTokenStore jwtTokenStore() {
return new JwtTokenStore(jwtAccessTokenConverter());
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients.inMemory()
.withClient("my-client-id")
.secret(passwordEncoder.encode("my-client-secret"))
.authorizedGrantTypes("password", "refresh_token")
.scopes("read", "write")
.accessTokenValiditySeconds(3600)
.refreshTokenValiditySeconds(86400);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager)
.userDetailsService(userDetailsService)
.accessTokenConverter(jwtAccessTokenConverter())
.tokenStore(jwtTokenStore());
}
}
```
在上面的配置类中,我们使用@EnableAuthorizationServer注释启用OAuth2,并实现了AuthorizationServerConfigurer接口以配置客户端和端点。具体来说:
- 我们配置了一个内存中的客户端,使用密码和刷新令牌授权类型,以及读写作用域。
- 我们还配置了JWT令牌转换器和令牌存储。
- 我们将身份验证管理器、用户详细信息服务、JWT令牌转换器和令牌存储配置为端点。
4. 实现用户详细信息服务
我们需要实现UserDetailsService接口来加载用户详细信息。以下是一个示例实现:
```
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
@Autowired
private UserRepository userRepository;
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found with username: " + username);
}
return new org.springframework.security.core.userdetails.User(
user.getUsername(), user.getPassword(), Collections.emptyList());
}
}
```
在上面的实现中,我们使用Spring Data JPA从数据库中加载用户,并创建一个SimpleGrantedAuthority对象列表作为用户的权限。
5. 实现密码编码器
我们需要实现PasswordEncoder接口,以便在创建用户时对密码进行编码。以下是一个示例实现:
```
@Service
public class PasswordEncoderImpl implements PasswordEncoder {
@Override
public String encode(CharSequence rawPassword) {
return BCrypt.hashpw(rawPassword.toString(), BCrypt.gensalt());
}
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
return BCrypt.checkpw(rawPassword.toString(), encodedPassword);
}
}
```
在上面的实现中,我们使用BCrypt编码算法对密码进行编码和验证。
6. 实现控制器
最后,我们需要实现一个控制器来测试OAuth2和JWT。以下是一个示例实现:
```
@RestController
@RequestMapping("/api")
public class ApiController {
@GetMapping("/hello")
public String hello() {
return "Hello, World!";
}
@GetMapping("/user")
public Principal user(Principal principal) {
return principal;
}
}
```
在上面的实现中,我们有两个端点:/api/hello和/api/user。前者返回一个简单的字符串,后者返回当前用户的Principal对象。
7. 测试应用程序
现在,您可以启动应用程序并使用以下步骤测试OAuth2和JWT:
- 获取访问令牌。使用以下curl命令以密码授权方式获取访问令牌:
```
curl -X POST \
-d 'grant_type=password&username=my-username&password=my-password' \
-H 'Authorization: Basic bXktY2xpZW50LWlkOm15LWNsaWVudC1zZWNyZXQ=' \
http://localhost:8080/oauth/token
```
在上面的命令中,my-username和my-password应该是您的用户名和密码,bXktY2xpZW50LWlkOm15LWNsaWVudC1zZWNyZXQ=应该是Base64编码的客户端ID和客户端密钥。
- 使用访问令牌访问API。使用以下curl命令使用JWT访问/api/hello端点:
```
curl -H 'Authorization: Bearer <access-token>' \
http://localhost:8080/api/hello
```
在上面的命令中,<access-token>应该是您在第一步中获取的访问令牌。
- 获取用户信息。使用以下curl命令使用JWT访问/api/user端点:
```
curl -H 'Authorization: Bearer <access-token>' \
http://localhost:8080/api/user
```
在上面的命令中,<access-token>应该是您在第一步中获取的访问令牌。