目前使用spring security,但如何试验都登录校验失败,debug跟踪发现实现 UserDetailsService 接口的 loadUserByUsername 方法,lrc下载参数总是空,求大神指点。
loadUserByUsername 默认参数名为 String username,页面提交参数名必须也为username,否则security取不到值
试验了一下,还是没有传值。
补个代码吧。项目是我再csdn下载的,应该本身没有问题。
JSP
<%@ page language=
"java"
import
=
"java.util.*"
pageEncoding=
"UTF-8"
%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+
"://"
+request.getServerName()+
":"
+request.getServerPort()+path+
"/"
;
%>
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
>
<html>
<head>
<base href=
"<%=basePath%>"
>
<title>登录</title>
<meta http-equiv=
"pragma"
content=
"no-cache"
>
<meta http-equiv=
"cache-control"
content=
"no-cache"
>
<meta http-equiv=
"expires"
content=
"0"
>
<meta http-equiv=
"keywords"
content=
"keyword1,keyword2,keyword3"
>
<meta http-equiv=
"description"
content=
"This is my page"
>
<!--
<link rel=
"stylesheet"
type=
"text/css"
href=
"styles.css"
>
-->
</head>
<body>
<h3>用户登录</h3>
${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
<form action=
"${pageContext.request.contextPath}/j_spring_security_check"
method=
"post"
prependId=
"false"
>
用户名:<input type=
"text"
name=
"j_username"
><br>
密码:<input type=
"password"
name=
"j_password"
>
<input type=
"submit"
value=
"登录"
>
</form>
</body>
</html>
用户名:<input type="text" name="j_username"><br>
name="username"
上面已经说过,试验过调整一致参数名,扔未传值,话说 spring security 要求传入用户名必须是 "j_username" 这个名称吧。至于loadUserByUsername只是形参而已,名称无所谓对应一致。
我也碰到这个问题了。加上这个就解决了
<security:form-login login-processing-url="/j_spring_security_check" username-parameter="j_username"/>
public UserDetails loadUserByUsername(String loginName) throws UsernameNotFoundException { } 这 个只能是获取到用户名,但是现在我想在这个地方拿到页面上的 角色 的参数,我该怎么获取呢,因为这个类中实现的是 implements UserDetailsService
将content-type改为 application/x-www-form-urlencoded 就对了
表单提交默认是application/x-www-form-urlencoded,但是后台改如何写?