【发布时间】:2019-12-27 22:17:21
【问题描述】:
我是 Spring Boot 新手,正在尝试找出将 终点。我已启用 Spring Security。
我有一个带有端点 Hello 的控制器类,它应该返回“hello” 作为响应,并希望任何人无需身份验证即可访问此端点。
@RestController
@RequestMapping(value = {"/employee"})
public class EmployeeController {
@Autowired
EmployeeRepository empRepose;
@Autowired
EmployeeService empService;
@Autowired
private Utility utility;
@PreAuthorize("permitAll()")
@GetMapping(value = "/hello", produces = MediaType.APPLICATION_JSON_VALUE)
public String home() {
return "Hello Employee!";
}
}
Spring 安全配置:
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
@EnableWebSecurity
public class ApplicationBasicAuth extends WebSecurityConfigurerAdapter {
@Autowired
RegisterUser beanRegisteruser;
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
/* httpSecurity.csrf().disable()
.authorizeRequests().anyRequest().authenticated()
.and().httpBasic();*/
/*httpSecurity
.httpBasic()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/employee/**").permitAll()
.and()
.csrf().disable();*/
httpSecurity.csrf().disable();
httpSecurity.authorizeRequests().anyRequest().permitAll();
}
我尝试了很多方法来将所有端点甚至 1 个我不需要进行身份验证的端点列入白名单。
请帮我找出我在这里做错了什么。
【问题讨论】:
-
您的代码应该可以工作。您必须将响应代码、错误消息和/或日志消息添加到您的问题中。您是在浏览器中输入应用程序的 URL 还是使用任何其他客户端?
-
嗨 Nikolai,我在浏览器和客户端上都试过了,浏览器将我重定向到登录页面,客户端给我 401 错误。
标签: rest spring-boot spring-security