这是我的主控制器:
package org.demian.demibox.controllers;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class MainController {
private String getUsername() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth.isAuthenticated())
return auth.getName();
else
return null;
}
@RequestMapping(value = "/", method = RequestMethod.GET)
public String showHome() {
String username = getUsername();
System.out.println(username);
if (username == null || username.length() == 0)
return "welcome";
return "index";
}
}
Run Code Online (Sandbox Code Playgroud)
即使我没有登录,也auth.isAuthenticated()总是返回true.这是为什么?何时会auth.isAuthenticated()返回虚假?经过身份验证的用户的名称是,anonymousUser如果我没有登录,并且如果我已登录,则使用用户名.
这是我的security-context.xml档案:
<?xml version="1.0" encoding="UTF-8"?>
<beans …Run Code Online (Sandbox Code Playgroud) 我有一个Spring Boot WebMVC应用程序,以及一个继承自AbstractPreAuthenticatedProcessingFilter的bean,我明确地将其添加到Spring Security过滤器链中的特定位置.我的Spring Security配置如下所示:
<http pattern="/rest/**">
<intercept-url pattern="/**" access="ROLE_USER"/>
<http-basic/>
<custom-filter after="BASIC_AUTH_FILTER" ref="preAuthenticationFilter"/>
</http>
<beans:bean id="preAuthenticationFilter" class="a.b.PreAuthenticationFilter">
<beans:property name="authenticationManager" ref="customAuthenticationManager"/>
</beans:bean>
Run Code Online (Sandbox Code Playgroud)
安全配置有效.问题是,因为PreAuthenticationFilter类继承自AbstractPreAuthenticatedProcessingFilter,所以Spring Boot将其视为通用servlet过滤器,并将其添加到servlet过滤器链中以用于所有请求.我不希望此过滤器成为所有请求的过滤器链的一部分.我只希望它成为我配置的特定Spring Security过滤器链的一部分.有没有办法阻止Spring Boot自动将preAuthenticationFilter bean添加到过滤器链?