1
回答
getAuthentication() 为何有时为空 有时不为空?空指针异常
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

我采用的springsecurity2

web.xml配置

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>
     classpath:/applicationContext.xml
    classpath:/applicationContext*.xml
     classpath:/security.xml  <!--    -->
   </param-value>
  </context-param> 
<!--    -->
  <filter>
     <filter-name>securityFilter</filter-name>
     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
     <init-param>
      <param-name>targetBeanName</param-name>
      <param-value>springSecurityFilterChain</param-value>
     </init-param>
  </filter> 
 
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter> 
 
 <!--   --> 
  <filter-mapping>
   <filter-name>securityFilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>

     <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
 
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
   <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>
   <listener>
   <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

security.xml中如下:

<intercept-url pattern="/admin/*" access="ROLE_ADMIN,ROLE_SERVICE,ROLE_KNOWLEDGE"/>
   <form-login login-page="/loginAdmin.jsp" authentication-failure-url="/loginAdmin.jsp?error=true" login-processing-url="/j_spring_check"
   default-target-url="/admin/index" always-use-default-target='true'/>

在admin/index下的action中 可以得到userDetails  或ManagementUser

ManagementUser mu =(ManagementUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
  System.out.println(""+mu);

但是在其他的时候访问 getAuthentication()就为空

举报
vitou
发帖于6年前 1回/6K+阅
顶部