春天安全:校长在哪里?

时间:2013-05-12 00:21:34

标签: spring-security principal

我一直在使用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时,我被提示登录。进入测试/测试后,我访问了forAuthenticated页面,但没有访问admin / adminUsers.jsp。这没关系,但问题是我无法访问index.jsp中的主体。这是index.jsp在以test / test身份登录时打印的内容。请注意,我看不到not logged inlogged in跟踪:

for authenticated
for admins 
Your principal object is....: 
null

Logout

我错过了什么?我该怎么做双击()?

1 个答案:

答案 0 :(得分:1)

正如Karthikeyan在评论中所说,问题在于以下几行:

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

有效地使匹配给定模式的请求直接发送到跳过Spring Security过滤器链的处理程序方法。在这种情况下,甚至SecurityContext都没有被初始化,因此身份验证对象将无法用于<sec:authorize>标签,根据该标签,它应该决定是否显示包装的内容(并且它显然赢了' t默认情况下。)

reference doc也明确说明了这一点:

  

通过将此属性(security)设置为none,可以将请求模式映射到空过滤器链。将不会应用任何安全性,并且Spring Security的所有功能都不可用。

您应该只允许匿名访问,而不是映射空的过滤器链:

<sec:intercept-url pattern="/index.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/>