【发布时间】:2019-09-25 23:44:01
【问题描述】:
我有一个带有多个控制器的 Web 服务。我只需要为其中一个添加身份验证。其余的应该保持没有身份验证。请注意,我只需要在请求标头中添加用户名和密码。没有登录表格。我正在使用 Spring Boot 应用程序。
我试过的代码:
@Configuration
public class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("{noop}pass").roles("ADMIN");
}
// Secure the endpoints with HTTP Basic authentication
@Override
protected void configure(HttpSecurity http) throws Exception {
http
//HTTP Basic authentication
.httpBasic()
.and()
.authorizeRequests()
.antMatchers(HttpMethod.GET, "/myController").hasRole("ADMIN")
.and()
.csrf().disable()
.formLogin().disable();
}
但是它不要求用户和密码。如果我删除 {noop} 它会但会抛出无效密码编码器的异常
【问题讨论】:
-
太好了,听起来您有一些要求要实现。但是,您还没有发布问题(只是要求)。请澄清您究竟有什么问题,并将任何相关代码发布到问题本身。例如,现在我不确定您是否在区分这些控制器时遇到问题,或者身份验证类型(使用请求标头)以及您到底遇到了什么问题。
标签: java rest spring-boot