Spring MVC 3.0 基本身份验证实现

2024-04-25

我目前正在使用 ASP.NET 的 Spring MVC 框架将我的 Web 应用程序转换为 Java(不过学习它的好方法 -:) )我需要在我的应用程序中实现身份验证:请告诉我我的方法是否足够好和专业,以及如果不是,最好的做法是什么:

首先,我正在编写 User 类,其中包含有关当前用户名字/姓氏/电子邮件/id/等的所有信息......

class User implements Serializable{
private String firstName;
private String lastName;
private Long id;
private String email;

///Settters and Getters

}

我正在实现名为 DlSession 的类并在会话级别实现它。

<bean id="MySession" class="DlSession" scope="session">
<aop:scoped-proxy/>

class DlSession implements Serializable{
private User currentUser;

public DlSession(){}

// getters and setters:
}

当用户提交他的用户/通行证时,我正在验证凭据,并且如果用户存在,则将所有用户数据检索到 User 类的实例。然后我将 Session 中的 currentUser 设置为我检索到的用户:

mySesison.setCurrentUser(user);

为了验证身份验证,我需要检查:

if (mySession.getcurrentUser() == null)
//return unauthenticated 
else 
//return authenticated

要从系统中注销用户,我只需执行以下操作:

mySession.setcurrentUser(null);

这种做法正确吗?任何建议都更受欢迎。 :)


如果您已经在使用SpringMVC,你为什么不也使用SpringSecurity http://static.springsource.org/spring-security/ (manual http://static.springsource.org/spring-security/site/docs/3.1.x/reference/springsecurity.html)? 它内置了您设置所需的所有组件基于表单或基本身份验证 http://static.springsource.org/spring-security/site/docs/3.1.x/reference/ns-config.html#ns-form-and-basic。而且,您将来可以轻松添加新的身份验证方法。

EDIT: 也可以看看这个问题 https://stackoverflow.com/q/4067736/74694对于可能的解决方案,使用 Spring Security。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring MVC 3.0 基本身份验证实现 的相关文章

随机推荐