1、登陆 Controller
@Controller
public class UserController {
@Autowired
private SysUserService sysUserService;
@RequestMapping("/login")
public void login(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
String username = request.getParameter("username");
String password = request.getParameter("password");
SysUser sysUser = sysUserService.findByKeyword(username);
String errorMsg = "";
if (StringUtils.isBlank(username)) {
errorMsg = "用户名不可以为空";
} else if (StringUtils.isBlank(password)) {
errorMsg = "密码不可以为空";
} else if (sysUser == null) {
errorMsg = "查询不到指定的用户";
} else if (!sysUser.getPassword().equals(MD5Util.encrypt(password))) {
errorMsg = "用户名或密码错误";
} else if (sysUser.getStasus() != 1) {
errorMsg = "用户已被冻结,请联系管理员";
} else {
// login success
request.getSession().setAttribute("user", sysUser);
response.sendRedirect("/admin/index");
return; // 防止提交后再次转向
}
request.setAttribute("error", errorMsg);
request.setAttribute("username", username);
String path = "/test/signin";
request.getRequestDispatcher(path).forward(request, response);
}
}
登陆成功跳转
@Controller
@RequestMapping("/admin")
public class AdminController {
@RequestMapping("/index")
public ModelAndView index(){
return new ModelAndView("admin/admin");
}
}
附:admin.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<#include "../common/backend_common.ftl">
</head>
<body class="no-skin">
<div id="navbar" class="navbar navbar-default">
<div class="navbar-container">
<button type="button" class="navbar-toggle menu-toggler pull-left" id="menu-toggler">
<span class="sr-only">Toggle sidebar</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<div class="navbar-header pull-left">
<a href="/admin/page.do" class="navbar-brand">
<small>
<i class="fa fa-leaf"></i>管理员控制台
</small>
</a>
</div>
<div class="navbar-buttons navbar-header pull-right" role="navigation">
<ul class="nav ace-nav">
<li class="light-blue dropdown-modal">
<a data-toggle="dropdown" href="#" class="dropdown-toggle">
<span class="user-info">
<small>欢迎,</small>
Admin
</span>
<i class="ace-icon fa fa-caret-down"></i>
</a>
<ul class="user-menu dropdown-menu-right dropdown-menu dropdown-yellow dropdown-caret dropdown-close">
<li>
<a href="#"> <i class="ace-icon fa fa-cog"></i>
设置
</a>
</li>
<li>
<a href="profile.html"> <i class="ace-icon fa fa-user"></i>
个人资料
</a>
</li>
<li class="divider"></li>
<li>
<a href="/logout.page"> <i class="ace-icon fa fa-power-off"></i>
注销
</a>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
<div class="main-container" id="main-container">
<script type="text/javascript">
try {
ace.settings.check('main-container', 'fixed')
} catch (e) {
}
</script>
<div id="sidebar" class="sidebar responsive">
<script type="text/javascript">
try {
ace.settings.check('sidebar', 'fixed')
} catch (e) {
}
</script>
<ul class="nav nav-list">
<li class="active">
<a class="popstyle" href="/sys/user/page.do" target="_blank">
<i class="menu-icon fa fa-tachometer"></i>
<span class="menu-text"> 首页 </span>
</a>
<b class="arrow"></b>
</li>
<li class="">
<a href="#" class="dropdown-toggle">
<i class="menu-icon fa fa-desktop"></i>
<span class="menu-text"> 权限管理 </span>
<b class="arrow fa fa-angle-down"></b>
</a>
<b class="arrow"></b>
<ul class="submenu">
<li class="">
<a class="popstyle" href="/sys/dept/page" target="_blank">
<i class="menu-icon fa fa-caret-right"></i>
用户管理
</a>
<b class="arrow"></b>
</li>
<li class="">
<a class="popstyle" href="/sys/role/role.page" target="_blank">
<i class="menu-icon fa fa-caret-right"></i>
角色管理
</a>
<b class="arrow"></b>
</li>
<li class="">
<a class="popstyle" href="/sys/aclModule/acl.page" target="_blank">
<i class="menu-icon fa fa-caret-right"></i>
权限管理
</a>
<b class="arrow"></b>
</li>
<li class="">
<a class="popstyle" href="/sys/log/log.page" target="_blank">
<i class="menu-icon fa fa-caret-right"></i>
权限更新记录
</a>
<b class="arrow"></b>
</li>
</ul>
</li>
<li class="">
<a href="#" class="dropdown-toggle">
<i class="menu-icon fa fa-desktop"></i>
<span class="menu-text"> 系统管理 </span>
<b class="arrow fa fa-angle-down"></b>
</a>
<b class="arrow"></b>
<ul class="submenu">
<li class="">
<a class="popstyle" href="/config/config.page" target="_blank">
<i class="menu-icon fa fa-caret-right"></i>
全局配置管理
</a>
<b class="arrow"></b>
</li>
</ul>
</li>
</ul>
<!-- /.nav-list -->
<div class="sidebar-toggle sidebar-collapse" id="sidebar-collapse">
<i class="ace-icon fa fa-angle-double-left" data-icon1="ace-icon fa fa-angle-double-left"
data-icon2="ace-icon fa fa-angle-double-right"></i>
</div>
<script type="text/javascript">
try {
ace.settings.check('sidebar', 'collapsed')
} catch (e) {
}
</script>
</div>
<div class="main-content">
<iframe id="innerFrame" src="/sys/dept/page" width="99%" style="min-height: 768px;"></iframe>
</div>
<!-- /.main-content -->
<a href="index.html#" id="btn-scroll-up" class="btn-scroll-up btn btn-sm btn-inverse display">
<i class="ace-icon fa fa-angle-double-up icon-only bigger-110"></i>
</a>
</div>
<!-- /.main-container -->
<!-- basic scripts -->
<!--[if !IE]> -->
<script src="/assets/js/jquery-2.1.0.min.js"></script>
<!-- <![endif]-->
<!--[if IE]>
<script src="/assets/js/jquery-1.11.0.min.js"></script>
<![endif]-->
<!--[if !IE]> -->
<script type="text/javascript">
window.jQuery || document.write("<script src='/assets/js/jquery.min.js'>" + "<" + "/script>");
</script>
<!-- <![endif]-->
<!--[if IE]>
<script type="text/javascript">
window.jQuery || document.write("<script src='/assets/js/jquery1x.min.js'>" + "<" + "/script>");
</script>
<![endif]-->
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
<!-- page specific plugin scripts -->
<!--[if lte IE 8]>
<script src="/assets/js/excanvas.min.js"></script>
<![endif]-->
<script src="/assets/js/jquery-ui.custom.min.js"></script>
<script src="/assets/js/jquery.ui.touch-punch.min.js"></script>
<script src="/assets/js/jquery.easypiechart.min.js"></script>
<script src="/assets/js/jquery.sparkline.min.js"></script>
<script src="/assets/js/flot/jquery.flot.min.js"></script>
<script src="/assets/js/flot/jquery.flot.pie.min.js"></script>
<script src="/assets/js/flot/jquery.flot.resize.min.js"></script>
<!-- ace scripts -->
<script src="/assets/js/ace-elements.min.js"></script>
<script src="/assets/js/ace.min.js"></script>
<script>
$(document).ready(function () {
$(".popstyle").removeAttr("target");
$(".popstyle").each(function () {
var $this = $(this)
tmp = $this.attr("href");
$this.attr("data", tmp);
$this.attr("href", "javascript:void(0)");
})
$(".popstyle").click(function () {
var $this = $(this)
$("iframe").attr(
'src',
$this.attr("data")
);
});
$(".direct").click(function () {
var $this = $(this)
$("iframe").attr(
'src',
$this.attr("data-value")
);
});
});
</script>
</body>
</html>
登陆失败跳转,返回到登陆页面
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping("/signin")
public ModelAndView hello() {
return new ModelAndView("signin/signin");
}
}
附:signin.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面,任何其他内容都*必须*跟随其后! -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="/favicon.ico">
<title>登陆</title>
<!-- Bootstrap core CSS -->
<link href="/bootstrap3.3.5/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="/css/signin.css" rel="stylesheet">
<!-- Just for debugging purposes. Don't actually copy these 2 lines! -->
<!--[if lt IE 9]><script src="js/ie8-responsive-file-warning.js"></script><![endif]-->
<script src="/js/ie-emulation-modes-warning.js"></script>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!--[if lt IE 9]>
<script src="/js/html5shiv.min.js"></script>
<script src="/js/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<form class="form-signin" action="/login" method="post">
<h2 class="form-signin-heading">请登陆</h2>
<label for="inputEmail" class="sr-only">邮箱/电话</label>
<input type="text" id="inputEmail" class="form-control" placeholder="Email/Telephone" name="username" required autofocus >
<label for="inputPassword" class="sr-only">密码</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" name="password" required >
<div class="checkbox" style="color: red;"> ${error!''} </div>
<button class="btn btn-lg btn-primary btn-block" type="submit">登 陆</button>
</form>
</div>
<!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
<script src="/js/ie10-viewport-bug-workaround.js"></script>
</body>
</html>
2、登出 Controller
@RequestMapping("/logout")
public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.getSession().invalidate();
String path = "/test/signin";
response.sendRedirect(path);
}