首页 > 代码库 > 小东西
小东西
<script type="text/javascript">
$(document).ready(function(){
$(‘button‘).click(function(){
var usernmae = $(‘#username‘).val();
var password = $(‘#password‘).val();
var url=‘<%=path%>/login‘;
var dataj={‘loginName‘:usernmae,‘password‘:password};
$.ajax({
type:‘POST‘,
url:url,
contentType:‘application/json;charset=utf-8‘,
dataType:‘json‘,
data:JSON.stringify(dataj),
success:function(data){
alert(data);
},
error:function(){
}
})
});
});
</script>
===============================================================================
@RequestMapping("/login")
public Object loginPost( @RequestBody User loginUser) throws Exception {
logger.info("login ");
if (StringUtils.isEmpty(loginUser.getLoginName())) {
throw new Exception("user name is null");
}
if (StringUtils.isEmpty(loginUser.getPassword())) {
throw new Exception("pwd is null");
}
Subject user = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(loginUser.getLoginName(), DigestUtils.md5Hex(loginUser.getPassword()).toCharArray());
token.setRememberMe(true);
try {
user.login(token);
} catch (UnknownAccountException e) {
throw new RuntimeException("the count not exit", e);
} catch (DisabledAccountException e) {
throw new RuntimeException("account can‘t use", e);
} catch (IncorrectCredentialsException e) {
throw new RuntimeException("pwd is wrong", e);
} catch (Throwable e) {
throw new RuntimeException("other exception", e);
}
return null;
}
小东西