Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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 请求未通过Spring安全链时访问身份验证对象_Java_Spring_Spring Security - Fatal编程技术网

Java 请求未通过Spring安全链时访问身份验证对象

Java 请求未通过Spring安全链时访问身份验证对象,java,spring,spring-security,Java,Spring,Spring Security,我想要实现的是一个不安全的页面(例如/索引),但是当经过身份验证时,它应该能够访问身份验证对象以显示登录的用户 根据Spring文档,身份验证对象在未通过安全过滤器链时不可用: 如果您希望在请求期间使用SecurityContext内容的内容,则它必须已通过安全筛选器链。否则,将不会填充SecurityContextHolder,并且内容将为空 但要禁用页面安全性,则禁用安全筛选器: <http pattern="/index" security="none"/> 与filte

我想要实现的是一个不安全的页面(例如/索引),但是当经过身份验证时,它应该能够访问身份验证对象以显示登录的用户

根据Spring文档,身份验证对象在未通过安全过滤器链时不可用:

如果您希望在请求期间使用SecurityContext内容的内容,则它必须已通过安全筛选器链。否则,将不会填充SecurityContextHolder,并且内容将为空

但要禁用页面安全性,则禁用安全筛选器:

<http pattern="/index" security="none"/>

与filters=“none”类似,这也将完全禁用该请求路径的安全筛选器链–因此,当在应用程序中处理请求时,Spring安全功能将不可用

我也不能使用
,因为这会授予所有经过身份验证的用户权限。因为当找不到身份验证对象时,
AbstractSecurityInterceptor
将抛出一个
AuthenticationCredentialsNotFoundException


作为Spring Security的新手,我如何构建一个可以访问SecurityContext的非安全页面?(使用带XML配置的Spring Security 3.2.9)

您可以使用
匿名身份验证筛选器
,当没有用户登录时,它将创建一个
匿名身份验证令牌
实例并将其分配给
SecurityContextHolder.getContext().setAuthentication()
。因此,您将不会获得
AuthenticationCredentialsNotFoundException
,并且可以执行一个简单的
instanceof
操作来检查用户是否登录或匿名

<bean id="anonymousAuthFilter"
      class="org.springframework.security.web.authentication.AnonymousAuthenticationFilter">
    <property name="key" value="anonymousUser"/>
    <property name="userAttribute" value="anonymousUser,ROLE_ANONYMOUS"/>
</bean>

<http pattern="/**">
    <intercept-url pattern="/index" access="permitAll"/>
    <custom-filter ref="anonymousAuthFilter" position="ANONYMOUS_FILTER" />
</http>