Spring Security 登录不起作用

Posted

技术标签:

【中文标题】Spring Security 登录不起作用【英文标题】:Spring Security login don't work 【发布时间】:2015-07-20 16:08:33 【问题描述】:

我有一个应用程序,您可以使用您的用户名和密码登录...每个用户都有一个角色。我使用休眠 ORM。

我的登录机制不起作用,为此我使用 Spring Security。这些是我的课程:

经理类:

package com.abt.fiifootballmanager.entity;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;

@Entity
@NamedQuery(name="Manager.findAll", query="SELECT m FROM Manager m")
public class Manager implements Serializable 
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long managerId;

    private String email;

    private String password;

    private String username;

    @ManyToMany
    @JoinTable
    private List<Role> roles;
    //+getters and setters 
    

角色类:

@Entity
@Table(name="\"Role\"")
@NamedQuery(name="Role.findAll", query="SELECT r FROM Role r")
public class Role implements Serializable 
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long roleId;

    private String role;

    @ManyToMany(mappedBy = "roles")
    private List<Manager> managers;
    //+getters and setters

这是我来自 security.xml 的身份验证管理器:

<authentication-manager>
    <authentication-provider>

        <jdbc-user-service data-source-ref="dataSource"
            authorities-by-username-query="select Manager.username, Role.role from Manager
                                           join Manager_Role on Manager.managerId = Manager_Role.MANAGERS_MANAGERID
                                           join Role on Manager_Role.roles_roleId = Role.roleId
                                           where Manager.username = ?"
            users-by-username-query="select username,password,enabled from Manager where name = ?" />

    </authentication-provider>

 <!-- because I use a many-to-many relationship, Hibernate create a new 
    table, named Manager_Role, with next columns: MANAGERS_MANAGERID and roles_roleId -->
</authentication-manager>

这是我的登录表单:

    <form class="form-signin" role="form" action="<spring:url value="/j_spring_security_check" />" method="POST">
    <h2 class="form-signin-heading">Please sign in</h2>
    <input type="text" name="j_username" class="form-control" placeholder="Username" required autofocus> 
    <input type="password" name="j_password" class="form-control" placeholder="Password" required> 
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

问题是,当我尝试使用数据库中的用户名和密码登录时,什么也没有发生......并且没有抛出异常。我怎样才能找出问题所在?

【问题讨论】:

【参考方案1】:

这不是 Spring Security,你必须阅读 Spring Security 并了解它的架构,还有一些实现和配置, 尝试按照这些教程进行操作

http://keylesson.com/index.php/java-ee/spring-security/

http://www.mkyong.com/tutorials/spring-security-tutorials/

【讨论】:

以上是关于Spring Security 登录不起作用的主要内容,如果未能解决你的问题,请参考以下文章

在 Icefaces 中使用 Spring Security 登录不起作用

Spring Security - 即使凭据正确,身份验证也不起作用

Spring-security-cas 插件单点注销不起作用

发生超时后调用 Servlet 方法(控制器)时,Spring Security 3.1 重定向到登录不起作用

Spring Security:多个 HTTP 配置不起作用

Spring-security:带有 COOKIE 的会话配置不起作用