可能的解决方案:
- 为所需的“ROLE_ADMIN”添加 URL 拦截器
/admin
- 将 的实例配置为截获 URL 并将用户身份验证为ROLE_ADMIN(如果用户提供适当的凭据)
org.springframework.security.web.authentication.www.BasicAuthenticationFilter
/admin
示例配置:
<security:intercept-url pattern="/admin" access="ROLE_ADMIN"/>
<bean id="basicAuthenticationEntryPoint"
class="org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint">
<property name="realmName"
value="WS realm"/>
</bean>
<bean id="basicAuthenticationProcessingFilter"
class="org.springframework.security.web.authentication.www.BasicAuthenticationFilter">
<property name="authenticationManager"
ref="authenticationManager"/>
<property name="authenticationEntryPoint"
ref="basicAuthenticationEntryPoint"/>
</bean>
注意:BasicAuthenticationFilter的默认实现是一个被动过滤器,即它只是在请求中查找一个基本的auth标头,如果它不存在 - 什么都不做。如果希望筛选器显式要求从客户端进行基本身份验证,则需要扩展默认实现以开始到身份验证入口点:
public class BasicAuthenticationFilter
extends org.springframework.security.web.authentication.www.BasicAuthenticationFilter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
final HttpServletRequest request = (HttpServletRequest) req;
final HttpServletResponse response = (HttpServletResponse) res;
String header = request.getHeader("Authorization");
if ((header != null) && header.startsWith("Basic ")) {
super.doFilter(req, res, chain);
} else {
getAuthenticationEntryPoint().commence(request, response, new AuthenticationCredentialsNotFoundException("Missing credentials"));
}
}
}
此外,您需要调整过滤器以仅应用于URL - 通过在方法中对其进行硬编码或通过提供适当的包装器bean。/admin
doFilter