为RestController配置Spring Boot身份验证,可以通过以下步骤:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
@EnableWebSecurity
注解启用Web安全配置,并继承WebSecurityConfigurerAdapter
。import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/api/**").authenticated() // 需要身份验证的API路径
.anyRequest().permitAll() // 其他路径允许访问
.and()
.httpBasic(); // 使用基本身份验证
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("admin")
.password("{noop}password") // 使用明文密码
.roles("ADMIN");
}
}
在上面的示例中,配置了一个需要身份验证的API路径/api/**
,其他路径允许访问,并且使用了基本身份验证方式。同时,配置了一个内存中的用户(admin/password),并赋予了ADMIN角色。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
// ...
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
PasswordEncoder passwordEncoder = passwordEncoder();
auth.inMemoryAuthentication()
.withUser("admin")
.password(passwordEncoder.encode("password")) // 使用加密后的密码
.roles("ADMIN");
}
}
在上述示例中,使用了BCryptPasswordEncoder
作为密码编码器,将密码进行加密处理。
@PreAuthorize
或@RolesAllowed
,来限制访问权限。@RestController
@RequestMapping("/api")
public class MyRestController {
@GetMapping("/protected")
@PreAuthorize("hasRole('ADMIN')")
public String protectedEndpoint() {
return "This is a protected endpoint.";
}
}
上述示例中,只有具有ADMIN角色的用户才能访问/api/protected
路径的接口。
注意:以上示例仅为演示如何为RestController配置Spring Boot身份验证。实际应用中,可能需要根据具体业务需求进行更复杂的权限控制和用户管理。另外,为了更好地保护系统安全,建议使用HTTPS来传输敏感数据。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云