Spring security 로그인 처리 (1)

북마크 추가

1.JSP

 

<form id="loginForm2">

 <label class="" for="exampleInputPassword2">EMAIL</label>

 

  <input type="email" class="form-control" id="exampleInputEmail2" placeholder="Enter email" name="j_username">

  <label class="" for="exampleInputPassword2" style="margin-top:10px;">Password</label>

  <input type="password" class="form-control" id="exampleInputPassword2" placeholder="Password" name="j_password">

  <button type="submit" class="btn btn-default pull-right" style="margin-top:10px;" value="Login" onclick="ajaxLogin2()"> 로그인</button>

</form> 

 

form action을 써도 되지만 그냥 ajax로 구현

 

2.script

 

function ajaxLogin2(){

$.ajax({

   

type: 'POST',

url: '/j_spring_security_check',

data: $("#loginForm2").serialize(),

dataType : 'json', 

   success : function(data) {

   

    location.href="/main";

   

   },error : function(data){

    alert("로그인실패");

   }

});

 

url은 /j_spring_security_check을 호출해야되고 name도 빨간색으로 정해진 이름으로 사용하는게 편함

security-context.xml에서 설정한

로그아웃은 ajax를 써서해도 되고 그냥 /j_spring_security_logout을 요청하면 로그아웃됨

function ajaxLogout(){

$.ajax({

   

type: 'POST',

url: '/j_spring_security_logout',

   success : function(data) {

   

    location.reload();

   },error : function(data){

    alert("실패");

   }

});

}

 

 

 

 

 

AD
관리자
2014-07-15 20:14
SHARE