2012-06-16 24 views
0

我使用spring_security_check创建了一个登录页面。
这里:
j_username无法使用spring_security_check访问

<form name='f' action="/sec_test/j_spring_security_check" method='POST'> 

    <table> 
     <tr> 
      <td>User:</td> 
      <td><input type="text" name="j_username" value=''> 
      </td> 
     </tr> 
     <tr> 
      <td>Password:</td> 
      <td><input type="password" name="j_password" /> 
      </td> 
     </tr> 
     <tr> 
      <td colspan='2'><input name="submit" type="submit" 
       value="submit" /> 
      </td> 
     </tr> 
     <tr> 
      <td colspan='2'><input name="reset" type="reset" /> 
      </td> 
     </tr> 
    </table> 
</form> 

但我不能访问为j_username之后。

我已经试过:

*的request.getParameter( “为j_username”)
* request.getAttribute( “为j_username”)
* request.getAttribute( “为j_username”)
* request.getUserPrincipal() .getName()
* request.getRemoteUser()

从所有这些,我得到的是空!

任何想法该怎么办?

+0

我要补充一点,我用 session.getAttribute(“SPRING_SECURITY_LAST_USERNAME”) ,仍然我得到空。 –

回答

2

Spring根据配置文件中配置的身份验证的类型为您执行身份验证。一旦验证成功,它将重定向到登录成功url(例如/login.htm)。因为它重定向到/login.htm,除非在重写认证过滤器中的请求中明确设置,否则您将不会获得任何请求参数(用户名/用户角色)。

成功认证后,spring将认证信息存储在安全上下文中,包括用户角色。您可以从SecurityContext访问这些信息。请参阅 - link

+0

非常感谢@sunil。 我试过** Authentication auth = SecurityContextHolder.getContext()。getAuthentication(); String name = auth.getName(); ** 它工作。 –

0

我很想看看你的spring配置文件。无论如何,看起来您在春季安全性实际上会为您执行此操作时尝试验证用户的凭据。如果你不确定如何开始,请阅读spring的文档,包括任何可以找到的在线教程。这里是我的春天的安全配置是什么样子:

<security:http auto-config="true" use-expressions="true" access-denied-page="/login.html"> 
    <security:form-login 
     login-page="/login.html" 
     authentication-failure-url="/loginfail.html" 
     default-target-url="/authenticate.html"/> 

    <security:logout 
     invalidate-session="true" 
     logout-success-url="/login.html" 
     logout-url="/logoff.html"/> 
</security:http> 

<bean id="securityDataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName" value="java:comp/env/security_DS"/> 
    <property name="resourceRef" value="true"/> 
</bean> 

<bean id="encoder" class="org.springframework.security.crypto.password.StandardPasswordEncoder" /> 

<security:authentication-manager> 
    <security:authentication-provider> 
     <security:password-encoder ref="encoder" /> 
     <security:jdbc-user-service 
      data-source-ref="securityDataSource" 
      authorities-by-username-query="SELECT l.user_name as username, r.role_name AS authority FROM login l join user_role ur on l.user_id = ur.user_id join role r on ur.role_id = r.role_id WHERE l.user_name = ?" 
      users-by-username-query="SELECT user_name as username, password_value AS password, active_flg AS enabled FROM login WHERE user_name = ?" 
     />   
    </security:authentication-provider> 
</security:authentication-manager> 

如果你想拥有访问权限的用户名泉已经验证用户的凭证后,做这样的事情:

@RequestMapping(value = { "/authenticate.html" }, method = { RequestMethod.GET, RequestMethod.HEAD }) 
public ModelAndView authenticateUser(final HttpServletRequest httpServletRequest, HttpSession httpSession, Authentication authentication) { 
    User user = (User) authentication.getPrincipal(); 
    String userName = user.getUsername(); 
    ... 

你可以看到,该请求将被转发到基于我在我的spring配置中指定的default-target-url的/authenticate.html方法。

+0

事实上,我并没有兴趣做任何事情,比如验证用户名/密码,当春天已经这样做。我想要的只是在登录页面之后的所有其他页面的顶部显示j_username。尽管如此,我仍然无法使用它。 –

+0

j_username不能通过名称“j_username”访问,所以不能通过request.getParameter(“j_username”)来访问。相反,您可以通过Authentication对象访问它,就像我之前提到的那样。请参阅(http://stackoverflow.com/questions/5042983/how-can-i-get-j-username-on-my-index-jsp-after-successful-authentication-with-j) –

+0

你能为我提供一个java版的答案?我之前回答了这个问题,而我什么都没有。 –