小编Mil*_*ong的帖子

Spring Boot 和 Spring Security,无法在 AuthenticationEntryPoint 中发送带有自定义消息的错误

我正在使用 spring boot 开发一个restful api,并将使用自定义的authenticationEntryPoint。这是代码

@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

    private static final Logger logger = LoggerFactory.getLogger(JwtAuthenticationEntryPoint.class);
    @Override
    public void commence(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse,
                         AuthenticationException e) throws IOException, ServletException {
        logger.error("Responding with unauthorized error. Message - {}", e.getMessage());
        httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
    }
}
Run Code Online (Sandbox Code Playgroud)

以及安全配置中的相关部分。

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
        securedEnabled = true,
        jsr250Enabled = true,
        prePostEnabled = true
)
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
   ...

    @Autowired
    private JwtAuthenticationEntryPoint unauthorizedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                .exceptionHandling()
                    .authenticationEntryPoint(unauthorizedHandler) …
Run Code Online (Sandbox Code Playgroud)

java spring spring-boot

3
推荐指数
1
解决办法
2447
查看次数

标签 统计

java ×1

spring ×1

spring-boot ×1