Spring MVC Login Demo1

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring MVC Login Demo1相关的知识,希望对你有一定的参考价值。

<html>

加上上一篇拦截器文章代码综合一下
<body>
<div>
<input type="hidden" id="txtHiddenCtx" value="${pageContext.request.contextPath}" />
用户名:<input type="text" id="txtuserid"/><br>
密码:<input type="password" id="txtpassword"/>
<input type="button" value="登录" onclick="loginc();"/>
</div>
<script type="text/javascript">
var ctx = $("#txtHiddenCtx").val();
function loginc() {
var paramData = new Object();
paramData.userid = $("#txtuserid").val();
paramData.password = $("#txtpassword").val();

    var jsonData = JSON.stringify(paramData);

    $.ajax({
        url : ctx + "/auth/loginc",
        type : "POST",
        async : true,
        // contentType:"application/x-www-form-urlencoded",
        contentType : "application/json;charset=utf-8",
        dataType : "json",
        data : jsonData,
        beforeSend : function(XMLHttpRequest) {
        },
        success : function(data, textStatus) {
            if(data.flag == 1){
                window.location.href = ctx + "/";
            }else{
                alert(data.msg);
            }
        },
        error : function(XMLHttpRequest, textStatus, errorThrown) {
            // 通常情况下textStatus 和errorThown只有其中一个有值

            alert("error: " + XMLHttpRequest.responseText);

        },
        complete : function(XMLHttpRequest, textStatus) {
        }
    });

}

/script>
@Controller
public class IndexController {

@RequestMapping(value="/")
public String index() {
    return "index";
}

}

@Controllerbr/>@RequestMapping(value="/auth")
public class AuthorizationController {

@RequestMapping(value="/loginc",method=RequestMethod.POST)
public void LoginPage(@RequestBody Map<String, Object> map,HttpServletRequest request,HttpServletResponse response) {
    Map<String,Object> result = new HashMap<String,Object>();
    String userid = String.valueOf(map.get("userid"));
    String password = String.valueOf(map.get("password"));
    if(StringUtils.isEmpty(userid)) {
        result.put("flag", 0);
        result.put("msg", "userid isEmpty");
         MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
    }
    if(StringUtils.isEmpty(password)) {
        result.put("flag", 0);
        result.put("msg", "password isEmpty");
         MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
    }
    if("590031i".equals(userid) && "123".equals(password)) {
        result.put("flag", 1);
        HttpSession session = request.getSession();
        session.setMaxInactiveInterval(1800);  //设置过期时间,单位是秒
        session.setAttribute("userid", userid);

    }else {
        result.put("flag", 0);
        result.put("msg", "id or password error.");
    }
     MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
}

@RequestMapping(value="/loginout",method=RequestMethod.POST)
public void LoginOut(@RequestBody Map<String, Object> map,HttpServletRequest request,HttpServletResponse response) {
    Map<String,Object> result = new HashMap<String,Object>();
    HttpSession session = request.getSession();
    session.invalidate();
    result.put("flag", 1);
     MvcHelper.WriteResponseInfo(response,result, request.getCharacterEncoding());
}

}



以上是关于Spring MVC Login Demo1的主要内容,如果未能解决你的问题,请参考以下文章

Spring MVC @Controller中转发或者重定向到其他页面的信息怎么携带和传递(Servlet API对象)HttpServletRequestHttpServletRespose(代码片

spring mvc怎么加入权限控制,在未登录前,任何访问url都跳转到login页面;登录成功

Spring MVC 与 Spring Security 的集成测试

shiro+springmvc集成时,配置loginUrl='login.jsp',但是login.jsp页面在WEB-INF 目录下,导致无法显示

登录后 Spring MVC 控制器重定向到某个 URL

Spring-security /login 重定向