使用WebSecurityConfigureAdapter的SpringBoot2.x中的java身份验证处理

xmakbtuz  于 2021-07-26  发布在  Java
关注(0)|答案(2)|浏览(275)

我使用的是springboot2.0.5,它对springsecurity的依赖性低于5.0.8

<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
 </dependency>

出于测试目的,我从rest控制器抛出一个异常,如下所示。

@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
        throw new org.springframework.security.authentication.BadCredentialsException("yoyo");
}

config类就是这样的,

@Configuration    
    @EnableWebSecurity
    public class BasicConfiguration extends WebSecurityConfigurerAdapter {

        @Override
        public void configure(WebSecurity web) throws Exception {
            web.ignoring().antMatchers("/error");

        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.csrf().disable().authorizeRequests().antMatchers("/error").permitAll();
        }
    }

我在spring boot文档中添加了/error,
springboot2.0与springsecurity的默认值没有太大的偏离,因此springboot1.5中绕过springsecurity的一些端点现在默认是安全的。其中包括错误端点和指向静态资源(如/css/、/js/、/images/、/webjars/、/**/favicon.ico)的路径。如果您想打开这些,您需要显式地配置它。
在SpringBoot应用程序类中也添加以下内容

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })

当我到达剩下的终点时,

{
    "timestamp": "2021-01-17T03:52:50.069+0000",
    "status": 403,
    "error": "Forbidden",
    "message": "Access Denied",
    "path": "/greeting"
}

但我希望401状态显示“信息”:“yoyo”如下所示,

{
    "timestamp": 2021-01-17T03:52:50.069+0000,
    "status": 401,
    "error": "Unauthorized",
    "message": "yoyo",
    "path": "/greeting"
}

我需要做什么样的改变才能得到回应

nimxete2

nimxete21#

添加 http.exceptionHandling().authenticationEntryPoint(..) 得到 Unauthorized 而不是 Forbidden 为了 error 现场。

@Configuration
@EnableWebSecurity
public class BasicConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/error");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests().antMatchers("/error").permitAll();

        http.exceptionHandling().authenticationEntryPoint((request, response, ex) -> {
            // You can add more logic to return different error codes
            response.sendError(401, "Unauthorized");
        });
    }
}

定义一个返回 ex.getMessage() 而不是 Access Denied 为了 message 现场。

@Component
public class CustomErrorAttributes extends DefaultErrorAttributes {
    @Override
    protected String getMessage(WebRequest webRequest, Throwable error) {
        return error.getMessage();
    }
}

输出:

{
    "timestamp": "2021-01-20T15:06:33.122+00:00",
    "status": 401,
    "error": "Unauthorized",
    "message": "yoyo",
    "path": "/greeting"
}
csbfibhn

csbfibhn2#

我猜您没有看到抛出的异常,因为“greeting”端点是安全的,Spring Security 自己处理它。
设置“/greeting”不安全,以便您可以访问它并获取自定义错误

@Override
protected void configure(HttpSecurity http) throws Exception {
     http.csrf().disable().authorizeRequests().antMatchers("/**").permitAll();
}

相关问题