请大神指教!spring-security UserDetailsService loadUserByUsername 参数为空问题

目前使用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">

扫描二维码关注公众号,回复: 9922352 查看本文章

<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,但是后台改如何写?

发布了79 篇原创文章 · 获赞 2 · 访问量 2271

猜你喜欢

转载自blog.csdn.net/liuji0517/article/details/104690171