我使用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 :(得分:2)
Spring会根据配置文件中配置的身份验证类型为您进行身份验证。身份验证成功后,它会重定向到登录成功URL(例如/login.htm)。因为它重定向到/login.htm,除非在覆盖认证过滤器中的请求中明确设置,否则不会获得任何请求参数(用户名/用户角色)。
成功认证后,spring会将经过身份验证的信息存储在安全上下文中,包括用户角色。您可以从SecurityContext访问此信息。请参阅 - link
答案 1 :(得分:0)
我有兴趣看看你的spring配置文件。无论如何,当Spring安全性实际上为您执行此操作时,您似乎正在尝试验证用户的凭据。如果您不确定如何开始,请阅读spring的文档,包括您可以找到的任何在线教程。这是我的spring security配置的样子:
<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>
如果您想在spring验证用户凭据后访问用户名,请执行以下操作:
@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方法。