Spring Hibernate用户登录无法正常工作

时间:2013-04-25 16:52:12

标签: mysql spring hibernate

我有一个复杂的数据库,由5个不同关系的表组成。到目前为止,我正在尝试使用此数据库实现身份验证和授权无效。

数据库中的表格:

自我引用类

   CREATE TABLE `person` (
`person_id` BIGINT(10) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NULL DEFAULT NULL,
`email` VARCHAR(255) NULL DEFAULT NULL,
`checker_id` BIGINT(20) DEFAULT NULL,
PRIMARY KEY (`person_id`),
FOREIGN KEY (`checker_id`) REFERENCES `person` (`person_id`)

用户表

   /*Table`users` */
 CREATE TABLE `users` (
 `person_id` bigint(20) NOT NULL,
 `username` varchar(255) DEFAULT NULL,
 `password` varchar(255) DEFAULT NULL,
 `enabled` tinyint(1) DEFAULT NULL,
 PRIMARY KEY (`person_id`),
 FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`)

作用

  /*Table `roles` */
  CREATE TABLE `roles` (
  `person_id` bigint(20) NOT NULL,
  `role` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`person_id`),
  FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`), 
  FOREIGN KEY (`person_id`) REFERENCES `users` (`person_id`) ON DELETE CASCADE ON   

  UPDATE CASCADE

另一个与Person类有两个onetomany关系的表:

    /*Table structure for table `modules` */
 CREATE TABLE `course` (
`course_id` bigint(20) NOT NULL AUTO_INCREMENT,
`course_code` varchar(255) DEFAULT NULL,
`course_name` varchar(255) DEFAULT NULL,
`lecturer_id` BIGINT(20) DEFAULT NULL,
`checker_id` BIGINT(20) DEFAULT NULL,
PRIMARY KEY (`module_id`),
FOREIGN KEY (`person_id`) REFERENCES `person` (`person_id`),
FOREIGN KEY (`checker_id`) REFERENCES `person` (`checker_id`)

以上所有内容都是我想要实现的。人员自引用表与“课程”表有关系。并且person id由User和Roles表继承。

Java类

 @Entity
 @Table(name = "person")
 @Inheritance(strategy=InheritanceType.JOINED)
 @Component
 public class Person implements Serializable{

  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = "person_id")
  private Long personId;


  @OneToMany    
  private Set<Module> modules;

  @ManyToOne(cascade={CascadeType.ALL})
  @JoinColumn(name="checker_id")
  private Person checker;

  @OneToMany(mappedBy="checker")
  private Set<Person> setters = new HashSet<Person>();


  public Person() {
  }

  public Person(String name, String email) {
    this.name = name;
    this.email = email;
 }

用户模型

 @Entity
 @Table(name = "users")
 @PrimaryKeyJoinColumn(name="person_id")
 @Component
 public class User extends Person implements UserDetails, Serializable{

 @OneToOne
 @JoinColumn(name="person_id")
 private Role role;

角色模型

   @Entity
@Table(name="roles")
@PrimaryKeyJoinColumn(name="person_id")
@Component
public class Role extends Person implements Serializable{

@OneToOne(mappedBy="role")
private User user;

我有一个必需的DAO和服务类,我在customUserDetailsS​​ervice中调用:

  @Override
  public UserDetails loadUserByUsername(String userName)
        throws UsernameNotFoundException {
    User user;
    try {
        user = userService.getUserByUserName(userName);
    } catch (Exception e) {
        throw new UsernameNotFoundException(
                "getUserByUserName returned null.");
    }
    List<String> authorities = userService.getRolesByUserName(userName);
    user.setUserAuthorities(authorities);
    return (UserDetails) user;
  }

问题是,现在当我运行应用程序并尝试登录时,错误页面不断出现。我不知道为什么。我希望从另一个角度来看待它。

编辑:

当我尝试用户名和密码时,它会检索错误页面而不是主页,堆栈跟踪中显示的所有内容都是Hibernate查询:

Hibernate: select user0_.person_id as person1_4_, user0_1_.checker_id as checker4_4_, 

user0_1_.email as email4_, user0_1_.name as name4_, user0_.enabled as enabled7_, 

user0_.password as password7_, user0_.username as username7_ from users user0_ inner 

join person user0_1_ on user0_.person_id=user0_1_.person_id where user0_.username=?

编辑2控制器:

  @Controller
  public class AuthenticationController {


@RequestMapping(value = "/", method = RequestMethod.GET)
public String getLoginPage(@RequestParam(value="error", required=false) boolean 

    error, ModelMap model) {
logger.debug("Show login page");

if (error == true) {

    model.put("error", "You have entered an invalid username or password!");
} else {
    model.put("error", "");
}

return "login";
    }

首页代码

   @RequestMapping(value = "/home", method = RequestMethod.GET)
public String homePage(ModelMap model, HttpServletRequest request) {
    try {

        User user = (User) SecurityContextHolder.getContext()
                .getAuthentication().getPrincipal();


        request.getSession().setAttribute("username",
                user.getUsername());

    } catch (Exception e) {
        e.printStackTrace();
    }

    return "home";
}

登录表单:

   <form action="<c:url value='j_spring_security_check' />" method="post" >

<label for="j_username">Username</label>
<input id="j_username" name="j_username" type="text" />

<label for="j_password">Password</label>
<input id="j_password" name="j_password" type="password" />

    <input  type="submit"  
     value="Login"/>                                

    </form>

0 个答案:

没有答案