Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将httpBasic与AuthenticationEntryPoint一起使用_Java_Spring Boot_Authentication_Spring Security - Fatal编程技术网

Java 将httpBasic与AuthenticationEntryPoint一起使用

Java 将httpBasic与AuthenticationEntryPoint一起使用,java,spring-boot,authentication,spring-security,Java,Spring Boot,Authentication,Spring Security,我将在spring boot中使用JWT身份验证。我可以从postman登录并访问api,如教程所示。然而,当我尝试在localhost:8080的浏览器上执行相同操作时,我得到了一个401未经授权的错误 本教程中使用的配置方法是 @Override protected void configure(HttpSecurity http) throws Exception { http.cors().and().csrf().disable() .authorizeReques

我将在spring boot中使用JWT身份验证。我可以从postman登录并访问api,如教程所示。然而,当我尝试在localhost:8080的浏览器上执行相同操作时,我得到了一个401未经授权的错误

本教程中使用的配置方法是

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors().and().csrf().disable()
      .authorizeRequests().antMatchers("/api/auth/**").permitAll().anyRequest()
      .authenticated().and().exceptionHandling()
      .authenticationEntryPoint(unauthorizedHandler).and()
      .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);

    http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
}
当我删除authenticationEntryPoint并添加httpbasic时,当访问localhost:8080时,浏览器中将显示登录对话框

@Override
protected void configure(HttpSecurity http) throws Exception { 
    http.cors().and().csrf().disable()
      .authorizeRequests().antMatchers("/api/auth/**").permitAll().anyRequest()
      .authenticated().and().exceptionHandling()
      .and().httpBasic().and().sessionManagement()
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
但当我尝试使用authenticationEntryPoint添加httpBasic时,不会显示登录对话框

@Override
protected void configure(HttpSecurity http) throws Exception {
      http.cors().and().csrf().disable()
        .authorizeRequests().antMatchers("/api/auth/**").permitAll().anyRequest()
        .authenticated().and().exceptionHandling()
        .authenticationEntryPoint(unauthorizedHandler)
        .and().httpBasic().and().sessionManagement()
        .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

  http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
}
已更新

unauthorizedHandler
是AuthenticationEntryPoint的一个实现

@Component
public class UnauthorizedHandler implements AuthenticationEntryPoint {

    private static final Logger logger = LoggerFactory.getLogger(UnauthorizedHandler.class);

    @Override
    public void commence(HttpServletRequest request,
                     HttpServletResponse response,
                     AuthenticationException e) 
                             throws IOException, ServletException {

        logger.error("Unauthorized error. Message - {}", e.getMessage());
        response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Error -> Unauthorized");
}
}


使用AuthenticationEntryPoint时如何显示登录对话框?

默认情况下,启用了异常处理功能,并且在设置
httpBasic()时使用默认入口点,即
http403禁止入口点
它在
异常处理
中设置新的默认入口点,即
基本身份验证入口点
——当定义了非显式入口点时,一切都与默认有关

当您显式定义新入口点时,
.exceptionHandling().authenticationEntryPoint(某些入口点)
不再使用所有默认配置,而是将使用
某些入口点

请注意,
httpBasic
仍在工作,但它应该以某种方式告诉您的浏览器身份验证失败,并告诉浏览器向用户请求http basic的凭据,而这正是
BasicAuthenticationEntryPoint
通过设置所做的

在您的
未授权处理程序中
开始
添加行
字符串realmName=“一些文本”
response.addHeader(“WWW-Authenticate”,“基本领域=\”“+realmName+”\”)


另外,我还没有分析你的案例,只是告诉你哪里出了问题(为什么不能按预期工作)以及如何让它工作-因此,从安全角度来看,这个解决方案可能不太好。

这有点陈旧,但我希望我的回答对其他人有所帮助

我遇到了完全相同的问题,因为我试图为swagger获取auth对话框,但我希望在不显示对话框的情况下处理未经授权的请求,因此我所做的是:

@组件
公共类AuthEntryPointJwt实现AuthenticationEntryPoint{
私有静态最终记录器Logger=LoggerFactory.getLogger(AuthEntryPointJwt.class);
@凌驾
公共无效开始(HttpServletRequest请求,HttpServletResponse响应,
AuthenticationException(authException)引发IOException{
//请求的URL包含“swagger”,因为它是我想在其中显示登录对话框的唯一页面,当设置标题以显示对话框并发送回响应时
if(request.getRequestURL().toString()包含(“招摇过市”)){
setHeader(“WWW-Authenticate”、“BASIC”);
response.senderor(response.SC_未经授权);
}否则{
//如果没有,只发送未经授权的错误
error(“未经授权的错误:{}”,authException.getMessage());
senderro(HttpServletResponse.SC_UNAUTHORIZED,“错误:UNAUTHORIZED”);
}
}
}

为什么需要自定义身份验证入口点?什么是未经授权的处理程序?显示您的代码
httpBasic
已经添加了一个身份验证入口点,这应该足够了。我已经更新了原始问题中的代码,添加了
未经授权的Handler
。您的身份验证点没有任何作用(只是记录)。移除它。