我一直在玩 spring-security 一段时间,由于某种原因,即使身份验证和授权正常工作,我也无法访问 JSP 中的主体。
这是我的 index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<%@ page import="org.springframework.security.core.context.SecurityContextHolder" %>
<a href="forAuthenticated.jsp">for authenticated</a><br/>
<a href="admin/adminUsers.jsp">for admins</a>
<sec:authorize access="! isAuthenticated()">
not logged in
</sec:authorize>
<sec:authorize access=" isAuthenticated()">
logged in
</sec:authorize>
Your principal object is....: <sec:authentication property="principal.username" /><br/>
Authentication = <%=SecurityContextHolder.getContext().getAuthentication() %>
<p><a href="j_spring_security_logout">Logout</a></p>
这是*-security-xml:
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http pattern="/loggedout.jsp" security="none"/>
<http pattern="/index.jsp" security="none"/>
<http pattern="/login.jsp" security="none"/>
<http auto-config="true">
<intercept-url pattern="/admin/*" access="ROLE_ADMIN,ROLE_SUPERUSER" />
<intercept-url pattern="/forAuthenticated.jsp" access="ROLE_USER" />
<intercept-url pattern="/logoutSuccess*" access="ROLE_ANONYMOUS" />
<intercept-url pattern="/**" access="ROLE_USER" />
<form-login login-page="/login.jsp"
authentication-failure-url = "/login.jsp?login_error=1"/>
<logout logout-success-url="/loggedout.jsp" delete-cookies="JSESSIONID"/>
<session-management invalid-session-url="/sessionTimeout.htm" />
</http>
<global-method-security pre-post-annotations="enabled"/>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<user-service id="userDetailsService">
<user name="username" password="password" authorities="ROLE_USER, ROLE_ADMIN" />
<user name="test" password="test" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
当我尝试访问 forAuthenticated.jsp 时,系统会提示我登录。进入 test/test 后,我访问了 forAuthenticated 页面,但没有访问 admin/adminUsers.jsp。这没问题,但问题是我无法访问 index.jsp 中的主体。这是以 test/test 登录时 index.jsp 打印的内容。请注意,我看不到not logged in
norlogged in
痕迹:
for authenticated
for admins
Your principal object is....:
null
Logout
我错过了什么?我应该双重检查()什么?