Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Security Spring3将UserDetails添加到会话_Security_Spring_Login - Fatal编程技术网

Security Spring3将UserDetails添加到会话

Security Spring3将UserDetails添加到会话,security,spring,login,Security,Spring,Login,我试图用Spring3实现一个DB驱动的用户登录设置。 我的日志显示,用户使用登录表单中提供的用户名/APSSOW进行了身份验证,但由于不再将用户硬编码到配置中,因此在JSP中似乎无法访问该用户 感觉UserDetails对象由于某种原因无法通过session/securitycontext使用 在my security-config.xml中: 旧的硬编码样式 <authentication-provider> <user-service>

我试图用Spring3实现一个DB驱动的用户登录设置。 我的日志显示,用户使用登录表单中提供的用户名/APSSOW进行了身份验证,但由于不再将用户硬编码到配置中,因此在JSP中似乎无法访问该用户

感觉UserDetails对象由于某种原因无法通过session/securitycontext使用

在my security-config.xml中: 旧的硬编码样式

    <authentication-provider>
    <user-service>
        <user name="reports" password="reports" authorities="ROLE_REPORTS" />
        <user name="admin" password="admin" authorities="ROLE_REPORTS, ROLE_ADMIN" />
        <user name="super" password="super" authorities="ROLE_REPORTS, ROLE_ADMIN, ROLE_SUPER_ADMIN"/>
    </user-service>
</authentication-provider>
//SecurityContext上下文=SecurityContextHolder.getContext(); ////context.setAuthentication(springUser); //System.out.println(“**LoginController用户验证确定:”+context.getAuthentication().getName()); //System.out.println(“**LoginController原则:“+context.getAuthentication().getPrincipal()”); }否则{ 返回登录错误(模型); }

    Collection<GrantedAuthority> roles = springUser.getAuthorities();
    for(GrantedAuthority auth : roles){
        if(auth.getAuthority().equals(Role.ROLE_ADMIN)){
            return "admin/home";
        }
    }

    model.addAttribute("message", "Logged in");
    return "reports/summaries";
}
Collection roles=springUser.getAuthorities();
for(授权授权:角色){
if(auth.getAuthority().equals(Role.Role\u ADMIN)){
返回“管理员/主页”;
}
}
model.addAttribute(“消息”、“登录”);
返回“报告/摘要”;
}
您将在上面我的控制器中看到,我想我希望能够在会话中访问UserDetails对象,但是那里的任何日志记录只返回字符串“anonymousUser”

报告/摘要jsp显示正常,但任何数据除外。JSP中包装在安全标记中的任何内容都不会显示。 例如,在以管理员用户身份成功登录后,缺少此区块:

        <sec:authorize access="hasRole('ROLE_ADMIN')">
        <li>
            <a href="<c:url value="/admin/home"/>">Admin</a>
            <ul>
                <li><a href="<c:url value="/admin/form/checkpoints"/>">Checkpoints</a></li>
                <li><a href="<c:url value="/admin/form/guards"/>">Guards</a></li>
                <li><a href="<c:url value="/admin/form/incidents"/>">Incidents</a></li>
                <li><a href="<c:url value="/admin/form/routes"/>">Routes</a></li>
            </ul>   
        </li>
    </sec:authorize>

  • 正如您现在可能已经收集到的,我是Spring/SpringMVC的新手。如果有人能为我推荐Spring身份验证框架的一些基本方面,那就太好了

    跟进/编辑: 我试着回到@NimChimpsky在评论中建议的形式风格。我在身份验证提供程序的设置中有两个选项,第一个选项有效:

    <authentication-provider>
            <user-service>
                <user name="reports" password="reports" authorities="ROLE_REPORTS" />
                <user name="admin" password="admin" authorities="ROLE_REPORTS, ROLE_ADMIN" />
                <user name="super" password="super" authorities="ROLE_REPORTS, ROLE_ADMIN, ROLE_SUPER_ADMIN"/>
            </user-service>
        </authentication-provider>
    
    
    
    第二个,它没有:

    <beans:bean id="userService" class="uk.co.romar.guardian.services.UserServiceImpl" />
    
    
    
    。。

    当我尝试使用UserServiceImpl类进行身份验证时,我在Hibernate sessionFactory上得到了一个NullPointerException,结果表明sessionFactory或我的其他服务都没有像我预期的那样被注入


    应该被注入的sessionfactorybean在我的hibernate-context.xml中定义,它通过servlet.xml配置中的import语句包含。但是,身份验证提供程序是在security-context.xml中声明的,它不引用hibernate-context.xml。因此,我将import语句复制到安全上下文中,希望能够对注入/实例化问题进行排序,但这也没有帮助。

    我认为您正在使它变得比需要的更复杂,spring为您完成所有工作。您不必为提交登录表单编写控制器。使用spring_security_登录,如下所示:

    <form name="f" id="f" action="<c:url value='j_spring_security_check'/>" method="POST">
      <label for="username">Username :&nbsp;</label>
      <input type='text' id='j_username' name='j_username'/><br>
      <label for="password">Password :&nbsp;</label>
      <input type='password' id='j_password' name='j_password'><br>
    
    
    用户名:
    
    密码:
    登录后(只需提交上述表单,即可访问jsp中的主体,导入:

    <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
    
    
    
    然后显示(或使用GetAuthority访问角色):

    用户名是
    
    谢谢@NimChimpsky,在添加用户服务之前,我使用的是那种没有控制器的表单。我认为控制器是在阅读了各种教程之后添加的。我现在遇到了一个不同的问题(SessionFactory没有被注入到用户服务中,我认为这与此无关),当我对其进行排序后,我将尝试返回此格式化添加上下文:对我的安全上下文的组件扫描似乎已解决了注入的其他问题。其他问题,但我将针对这些问题提出新的问题。
    <beans:bean id="userService" class="uk.co.romar.guardian.services.UserServiceImpl" />
    
    <form name="f" id="f" action="<c:url value='j_spring_security_check'/>" method="POST">
      <label for="username">Username :&nbsp;</label>
      <input type='text' id='j_username' name='j_username'/><br>
      <label for="password">Password :&nbsp;</label>
      <input type='password' id='j_password' name='j_password'><br>
    
    <%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>
    
    Username is  <sec:authentication property="principal.username" />