如何在spring中实现安全登录和注销?

时间:2014-04-11 09:07:39

标签: spring spring-mvc spring-security

请仔细阅读以下代码:

我一直在使用以下类来验证用户身份:

    @Controller
    public class AdminLoginController
    {

     @RequestMapping(value = "/loginForm", method ={RequestMethod.GET,RequestMethod.POST})
           public String showForm(ModelMap model) 
           {
    return  GlobalConstants.LOGIN_PAGE;}


    @RequestMapping(value = "/login" ,method ={RequestMethod.POST, RequestMethod.GET})
    public String processForm(@ModelAttribute("loginForm")AdminLoginForm loginForm, BindingResult result , HttpServletRequest request, HttpServletResponse response, ModelMap model)
    {

try{
AdminLoginWorker worker=new AdminLoginWorker();
        boolean status=worker.validateUser(loginForm);
if(status)
        {
if("superAdmin".equalsIgnoreCase(loginForm.getUserType()))
                {               
                    dtoBean.setEmp_id(loginForm.getUserName());
                    session.setAttribute("dtoBean", dtoBean);
                    return GlobalConstants.SUPERADMIN_HOME_PAGE;
                }

                if("Admin".equalsIgnoreCase(loginForm.getUserType()))
                {
                    dtoBean.setEmp_id(loginForm.getUserName());
                    session.setAttribute("dtoBean", dtoBean);
                    return GlobalConstants.HOME_PAGE;
                }
}catch(Exception e){

        e.printStackTrace();
    }
        return GlobalConstants.LOGIN_PAGE;
    }

并注销:

@RequestMapping(value = "/logout", method ={RequestMethod.GET,RequestMethod.POST})
       public ModelAndView logoutForm(HttpServletRequest request) 
       {

         HttpSession session = request.getSession(false);
            session.invalidate();
        return new ModelAndView( GlobalConstants.LOGIN_PAGE);
       }

我使用以下方法调用DAO方法从数据库进行验证:

    public class AdminLoginWorker{
    public boolean validateUser(AdminLoginForm loginForm){

        try{    con=DBConnection.getConnection();           
            query="select userType from login where emp_id=? and pwd=?";
                    pstmt.setInt(1,loginForm.getUserName());
            pstmt.setString(2,loginForm.getPassword());
if(rs.next()){               
             loginForm.setUserType(rs.getString(1));
             return true;}

所以现在我想在我的Web应用程序中实现spring security,所以我在spring-context.xml中尝试了以下内容:

<http auto-config="true">
        <intercept-url pattern="/**" access="isAuthenticated"/>  <!-- this  means all URL in this app will be checked if user is authenticated -->
        <form-login/> <!--  -->
        <logout logout-url="/logout" logout-success-url=""/>
 </http>
 <authentication-manager>
        <authentication-provider> 
            <user-service>
                <user name="sachin" password="sachin123" authorities="Admin"/>
            </user-service>
 </authentication-provider>

在上面的security-context.xml文件中,我想添加数据源,以便使用数据库验证用户,因此我一直使用下面的DBCOnnection类与Mysql连接:

public class DBConnection {

    private static Connection con=null;
    public static Connection getConnection()
    {
        try{
            if(con==null){
                Class.forName("com.mysql.jdbc.Driver");
                 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/portal",
                    "root", "root");
        }
        }catch(Exception e){
            e.printStackTrace();
        }

        return con;
    }
}

现在的问题是,

如何将上述数据源放在security-context.xml文件或

有什么办法可以从security-context.xml文件引用到DBConnection类来实现spring security login身份验证。

有没有人有想法解决这个问题?

1 个答案:

答案 0 :(得分:0)

在我看来,你正在进行相当数量的车轮改造。您尝试手动执行的所有操作都可以通过Spring Security轻松处理,无需编写代码,只需使用适当的配置即可。

查看thisthis博文,了解更多详情