Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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 使用JSR-250(@RolesAllowed)注释Spring MVC和容器身份验证_Java_Spring Mvc_Jakarta Ee_Spring Security - Fatal编程技术网

Java 使用JSR-250(@RolesAllowed)注释Spring MVC和容器身份验证

Java 使用JSR-250(@RolesAllowed)注释Spring MVC和容器身份验证,java,spring-mvc,jakarta-ee,spring-security,Java,Spring Mvc,Jakarta Ee,Spring Security,我有一个web应用程序,它使用Spring框架和SpringMVC来创建REST端点。容器使用JavaEE安全性(特别是由Active Directory支持的Wildfly 9中的安全领域)提供身份验证,并从应用程序数据库加载角色。我有一个过滤器应用的请求包装器,通过普通的ServletAPI提供用户详细信息 web.xml: 未经验证的资源 /favicon.ico /NoAccess.html /登录 /css/* /字体/* /图像/* /js/* /解放党/* /分部/* 所有资源

我有一个web应用程序,它使用Spring框架和SpringMVC来创建REST端点。容器使用JavaEE安全性(特别是由Active Directory支持的Wildfly 9中的安全领域)提供身份验证,并从应用程序数据库加载角色。我有一个过滤器应用的请求包装器,通过普通的ServletAPI提供用户详细信息

web.xml:


未经验证的资源
/favicon.ico
/NoAccess.html
/登录
/css/*
/字体/*
/图像/*
/js/*
/解放党/*
/分部/*
所有资源
/*
但我总是犯同样的错误。似乎过滤器从未添加到过滤器链中

我读过很多教程和帖子,但它们都说了同样的话,对我来说并不适用。我肯定遗漏了一些简单的东西——毕竟,所有的信息都在那里,Spring Security可以做出授权决定。有可能(可能?)我这里遗漏了一些简单的东西。非常感谢您的帮助

编辑

我已将以下代码添加到我的
SecurityFilter
类中,以填充Spring安全上下文。这是可行的,但我觉得这不是理想的解决方案

List<GrantedAuthority> authorities = roles.stream()
        .map(SimpleGrantedAuthority::new)
        .collect(Collectors.toList());
PreAuthenticatedAuthenticationToken auth = new PreAuthenticatedAuthenticationToken(user, "", authorities);
auth.setDetails(new WebAuthenticationDetails(wrappedRequest));
auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);
List authorities=roles.stream()
.map(SimpleGrantedAuthority::新建)
.collect(Collectors.toList());
PreAuthenticateAuthenticationToken auth=新的PreAuthenticateAuthenticationToken(用户“”,权限);
auth.setDetails(新的WebAuthenticationDetails(wrappedRequest));
auth.setAuthenticated(true);
SecurityContextHolder.getContext().setAuthentication(auth);

问题在于配置的身份验证提供程序。您使用的是,它从authenticate方法返回现有的身份验证令牌。因此,如果主体为null,那么它将忽略请求,并让其他提供者对其进行身份验证

鉴定

公共身份验证(身份验证) 引发AuthenticationException对给定的预身份验证TokenticationToken进行身份验证。如果 身份验证对象中包含的主体为null,请求 将被忽略以允许其他提供程序对其进行身份验证


基本上,您需要配置另一个AuthenticationProvider,它将生成身份验证令牌并在SecurityContext中设置此令牌。

但是查看J2eePreAuthenticatedProcessingFilter的源代码可以看出它从HttpRequest创建身份验证对象并将其放入安全上下文中(通过AbstractPreAuthenticatedProcessingFilter)。这难道不是我需要的全部吗?我查看了J2eePreAuthenticatedProcessingFilter的源代码,它没有创建身份验证对象。它只是从HttpRequest返回用户主体。请参阅(J2eePreAuthenticatedProcessingFilter的超类)好的。我的错。我没有看超类。你能调试这个AbstractPreAuthenticatedProcessingFilter并看看你在authResult中得到了什么吗?另外,J2eePreAuthenticatedProcessingFilter文档中提到没有通用的方法来检索凭证,比如getPreAuthenticatedCredentials方法返回一个修复了伪值,这可能是AuthenticationCredentialsNotFoundException背后的原因。我现在已经解决了这个问题(尽管仍然好奇为什么它不起作用)但我非常感谢您的帮助。请参阅问题编辑部分中的代码,这是我从AbstractPreAuthenticatedProcessingFilter中获取的。我设置了一个空白凭据字符串,但它仍然有效。在测试AbstractPreAuthenticatedProcessingFilter时,从未调用设置AbstractPreAuthenticatedProcessingFilter身份验证的代码(它从未到达我的断点)。