2012-11-27 58 views
1

我在我的spring安全配置中使用了多个http元素[具有不同模式]。每个指向一个单独的认证管理器。我能够成功登录所有http元素。但是,成功登录后,返回的Principal对象为null。请帮帮我。是成功登录后getPrincipal()返回null

弹簧安全的内容如下

<http pattern="teacher/login*" authentication-manager- 
     ref="teacherAuthenticationManager"> 
     <intercept-url pattern="teacher/login*" access="ROLE_TEACHER" /> 
      <http-basic`enter code here` /> 
     </http> 
     <http pattern="student/login*" authentication-manager- 
     ref="studentAuthenticationManager"> 
     <intercept-url pattern="student/login*" access="ROLE_STUDENT" /> 
     <http-basic /> 
     </http> 
     <authentication-manager alias="teacherAuthenticationManager"> 
     <authentication-provider> 
     <!-- <password-encoder hash="md5"/>--> 
     <jdbc-user-service data-source-ref="dataSources" 
     users-by-username-query=" 
      select username,password,true 
       from Teacher where username=?" 

      authorities-by-username-query=" 
      select username,'ROLE_TEACHER' from Teacher where username=?" /> 
     </authentication-provider> 
     </authentication-manager> 


     <authentication-manager alias="studentAuthenticationManager"> 
      <authentication-provider> 
     <!-- <password-encoder hash="md5"/>--> 
     <jdbc-user-service data-source-ref="dataSources" 
     users-by-username-query=" 
      select username,password,true 
      from Student where username=?" 

     authorities-by-username-query=" 
       select username,'ROLE_STUDENT' from Student where username=?" /> 
      </authentication-provider> 
     </authentication-manager> 

Web.xml中是如下

<display-name>Spring Web MVC Application</display-name> 
    <welcome-file-list> 
     <welcome-file>/index.html</welcome-file> 
    </welcome-file-list> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      /WEB-INF/mvc-dispatcher-servlet.xml, 
      /WEB-INF/spring-security.xml 
     </param-value> 
    </context-param> 

    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <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> 
     <dispatcher>FORWARD</dispatcher> 
       <dispatcher>REQUEST</dispatcher> 
       <dispatcher>ERROR</dispatcher> 
    </filter-mapping> 

控制器代码

@RequestMapping(value = "/teacher/login", method = RequestMethod.GET) 
    public @ResponseBody MethodResponse teacherlogin(Principal principal) { 
     System.out.println("Welcome Teacher"); 
     MethodResponse methodResponse = new MethodResponse(); 
     try { 
      //org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); 


        System.out.println("Is Principal Null:"+Boolean.valueOf(principal==null)); 
        final String name = principal.getName(); 

        String sql="Select * from Teacher where UserName=?"; 
        Teacher teacher = jdbcTemplate.queryForObject(sql, 
          ParameterizedBeanPropertyRowMapper 
            .newInstance(Teacher.class),name); 


        methodResponse.setData(teacher); 




      //String sql = " Select * from Teacher where TeacherId=?"; 

      /* 
      List<Teacher> list = jdbcTemplate.query(sql, 
        ParameterizedBeanPropertyRowMapper 
          .newInstance(Teacher.class), teacherId); 

      Teacher[] teachers = list.toArray(new Teacher[] {}); 
      methodResponse.setDataArray(teachers);*/ 

      methodResponse 
        .setResponseCode(GlobalConstants.SERVICE_STATUS_CODE_SUCCESS); 
      methodResponse 
        .setResponseMessage(GlobalConstants.SERVICE_STATUS_MSG_SUCCESS); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      methodResponse 
        .setResponseCode(GlobalConstants.SERVICE_STATUS_CODE_DATABASE_ERROR); 
      methodResponse.setResponseMessage(e.getMessage()); 
     } 
     return methodResponse; 
    } 
+0

N.B.你正在加载你的servlet XML两次(可能无害,但可能不是你想要的)。 –

回答

10

该servlet映射到/ rest/*,这些URL不受您的过滤器保护(所以我希望主体为空)。这是否解释你看到的行为?

+0

嗨戴夫,它工作得很好。非常感谢你的回答 – user998556

相关问题