为什么Spring Security getPrincipal提供NullPointer

问题描述

我正在尝试让登录用户在jsf索引页面显示其信息,您需要登录才能访问它,但无法访问登录页面,因为异常阻止了项目的正常运行,例如标题说我正在得到nullpointer异常,我正在使用java 4的Spring 4。

托管bean:

    @postconstruct
public void init() {
    Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    if (principal instanceof UserDetails) {
       login = ((UserDetails)principal).getUsername();
    } else {
       login = principal.toString();
    }

网络xml:

         <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
      </listener>
      <listener>
        <listener-class>
            org.springframework.web.context.request.RequestContextListener
        </listener-class>
      </listener>
        <!-- CONTEXT  -->
            <context-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>/WEB-INF/ApplicationContext.xml,/WEB-INF/spring-security.xml</param-value>
      
            </context-param>   

   <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

Spring security xml:

  <http 
    authentication-manager-ref="jdbc-auth"
      use-expressions="true" >   
                      <intercept-url pattern="/loggedout.xhtml" access="permitAll" />     
            <intercept-url pattern="/login.xhtml" access="permitAll" />
            <intercept-url pattern="/denied.xhtml" access="isAuthenticated()" />             
            <intercept-url pattern="/member/**" access="isAuthenticated()" />
            <intercept-url pattern="/admin/**" access="Administrateur-portail" />  
               
             <form-login  login-page="/login.xhtml" login-processing-url="/j_spring_security_check"
              username-parameter="username" password-parameter="password" authentication-failure-url="/denied.xhtml" />
    <csrf disabled="true"/>

    </http>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)