Commit 9f52ee39 by shj

登录拦截完成,用户未登录时只能查看登录页面,登录之后自动跳转到首页,即创建预算页面,首页增加退出登录按钮

parent cd8efb02
......@@ -4,4 +4,6 @@ import beans.User;
public interface IUserDao {
User selectUserByName(String name);
void insertUser(User user);
}
......@@ -5,4 +5,8 @@
<select id="selectUserByName" resultType="beans.User">
select * from user where name=#{name}
</select>
<insert id="insertUser" useGeneratedKeys="true" keyProperty="id">
insert into user(name,password) values(#{name},#{password})
</insert>
</mapper>
\ No newline at end of file
......@@ -110,12 +110,12 @@ public class BudgetHandler {
String filePath = getFilePath(Long.toString(sessionID));
serializeBudget(budget, filePath);
response.setHeader("content-disposition", "attachment;filename=Budget" + sessionID + ".csv");
//response.setHeader("content-disposition", "attachment;filename=Budget" + sessionID + ".csv");
System.out.println("ContextPath: " + request.getContextPath());
System.out.println(session.getServletContext().getRealPath(""));
budgetToOutputStream(budget, response.getOutputStream());
//budgetToOutputStream(budget, response.getOutputStream());
} catch (IOException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
......@@ -492,11 +492,8 @@ public class BudgetHandler {
/**
* 修改预算中的劳务费、规则中的劳务费
*
* @param mode
* @param id
* @param name
* @param price
* @param consultation
* @param nums
* @param curd
* @param request
......
......@@ -3,12 +3,16 @@ package handlers;
import beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import service.IUserService;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
public class LoginHandler {
......@@ -17,17 +21,63 @@ public class LoginHandler {
return "login.jsp";
}
@RequestMapping("/")
public String indexPage(){
System.out.println("show index");
return "index.jsp";
}
@Autowired
private IUserService userService;
@RequestMapping("/Login")
public void loginAction(User user,HttpServletRequest request, HttpServletResponse response){
public void loginAction(User user,HttpServletRequest request, HttpServletResponse response) throws IOException {
if(userService.checkUser(user))
{
response.addCookie(new Cookie("userid",user.getId().toString()));
response.addCookie(new Cookie("username",user.getName()));
//response.sendRedirect(request.getContextPath()+"/");
System.out.println("back to index page");
}
else {
;
response.sendError(444,"no such user");
}
}
/**
* 注册用户
* @param user
* @param request
* @param response
* @throws IOException
*/
@RequestMapping("/Register")
public void registerAction(User user,HttpServletRequest request,HttpServletResponse response) throws IOException {
if(userService.existUser(user)){
response.sendError(444,"username already exists.");
return;
}
userService.addUser(user);
response.addCookie(new Cookie("userid",user.getId().toString()));
response.addCookie(new Cookie("username",user.getName()));
System.out.println("register done");
}
/**
* 退出登录
* @param request
* @param response
* @throws IOException
*/
@RequestMapping("/Logout")
public void logoutAction(HttpServletRequest request,HttpServletResponse response) throws IOException {
Cookie cookie=new Cookie("userid",null);
cookie.setMaxAge(0);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
cookie=new Cookie("username",null);
cookie.setMaxAge(0);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
response.sendRedirect(request.getContextPath());
}
}
......@@ -11,19 +11,20 @@ public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
if(!checkCookie(request.getCookies(),"userid"))
{
request.getRequestDispatcher("/LoginPage").forward(request,response);
if (!checkCookie(request.getCookies(), "userid")) {
request.getRequestDispatcher("/LoginPage").forward(request, response);
//response.sendRedirect(request.getContextPath()+"/login.jsp");
return false;
}
return true;
}
private boolean checkCookie(Cookie[] cookies, String name) {
for (Cookie cookie : cookies) {
if(cookie.getName().equals(name))
return true;
}
if (cookies != null)
for (Cookie cookie : cookies) {
if (cookie.getName().equals(name))
return true;
}
return false;
}
......
package service;
/**
* 维护用户与预算文件的关系
*/
public interface IUserBudgetService {
}
......@@ -5,4 +5,8 @@ import beans.User;
public interface IUserService {
boolean checkUser(User user);
boolean existUser(User user);
void addUser(User user);
}
package service.impl;
import service.IUserBudgetService;
public class UserBudgetService implements IUserBudgetService {
}
......@@ -11,9 +11,22 @@ public class UserService implements IUserService {
private IUserDao userDao;
@Override
public boolean checkUser(User user) {
if(user==null) return false;
User exist=userDao.selectUserByName(user.getName());
if(exist==null) return false;
if(exist==null || !exist.getPassword().equals(user.getPassword())) return false;
user.setId(exist.getId());
return true;
}
@Override
public boolean existUser(User user) {
User exist = userDao.selectUserByName(user.getName());
return exist!=null;
}
@Override
public void addUser(User user) {
if(user==null) return ;
userDao.insertUser(user);
}
}
......@@ -20,6 +20,7 @@
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mapping path="/Login*"/>
<mvc:exclude-mapping path="/Register*"/>
<bean class="interceptors.LoginInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
......
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Index</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- 可选的 Bootstrap 主题文件(一般不用引入) -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css"
integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<style type="text/css">
body{
padding-top: 70px;
}
</style>
</head>
<body>
<nav class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">预算辅助管理系统</a>
</div>
<div>
<ul class="nav navbar-nav">
<li class="active"><a href="#">创建预算</a></li>
<li>
<a href="${pageContext.request.contextPath}/Budget/Detail" >
修改预算
</a>
</li>
<li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li>
<li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li>
<li><a href="${pageContext.request.contextPath}/Test">测试</a></li>
</ul>
</div>
</div>
</nav>
<%
//一些默认值
int total = 150,
equiment = 20,
material = 12,
test = 15,
power = 0,
travel = 12,
conference = 5,
international = 6,
property = 8,
labour = 66,
consultation = 2,
others = 0; %>
<form class="form-horizontal" method="post" action="${pageContext.request.contextPath}/Budget/Generate">
<div class="form-group">
<label class="control-label col-sm-4">总预算(万元)</label>
<div class="col-sm-4">
<input name="total" type="number" placeholder="" class="form-control" required value=<%=total%>>
</div>
</div>
<hr>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="equipment" checked>
设备费
</label>
<div class="col-sm-4">
<input name="equipment-number" class="form-control" type="number" value=<%=equiment%>>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="material" checked>
材料费
</label>
<div class="col-sm-4">
<input name="material-number" class="form-control" type="number" value=<%=material%>>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="test-and-process" checked>
测试化验加工费
</label>
<div class="col-sm-4">
<input name="test-and-process-number" class="form-control" type="number" value=<%=test%>>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="power" checked>
燃料动力费
</label>
<div class="col-sm-4">
<input name="power-number" class="form-control" type="number" value=<%=power%>>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="travel" checked>
差旅费
</label>
<div class="col-sm-4">
<input name="travel-number" class="form-control" type="number" value=<%=travel%>>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="conference" checked>
会议费
</label>
<div class="col-sm-4" id="tip">
<input name="conference-number" class="form-control" type="number" value=<%=conference%>>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="international-communication" checked>
国际合作交流费
</label>
<div class="col-sm-4">
<input name="international-communication-number" class="form-control" type="number"
value="<%=international%>"></div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="property" checked>
产权费
</label>
<div class="col-sm-4">
<input name="property-number" class="form-control" type="number" value="<%=property%>"></div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="labour" checked>
劳务费
</label>
<div class="col-sm-4">
<input name="labour-number" class="form-control" type="number" value="<%=labour%>"></div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="consultation" checked>
咨询费
</label>
<div class="col-sm-4">
<input name="consultation-number" class="form-control" type="number" value="<%=consultation%>"></div>
</div>
<div class="form-group">
<label class="control-label col-sm-4">
<input name="items" type="checkbox" value="others" checked>
其他费用
</label>
<div class="col-sm-4">
<input name="others-number" class="form-control" type="number" value="<%=others%>"></div>
</div>
<!-- Button -->
<div>
<button class="btn btn-default btn-lg center-block" type="submit">生成预算</button>
</div>
</form>
</body>
</html>
......@@ -67,9 +67,7 @@
<taglib-location>/WEB-INF/x.tld</taglib-location>
</taglib>
</jsp-config>
<!--<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>-->
<welcome-file-list>
<welcome-file/>
</welcome-file-list>
</web-app>
\ No newline at end of file
......@@ -45,6 +45,7 @@
<li><a href="${pageContext.request.contextPath}/Rule/">修改规则</a></li>
<li><a href="${pageContext.request.contextPath}/Budget/Download">导出最新预算</a></li>
<li><a href="${pageContext.request.contextPath}/Test">测试</a></li>
<li><a href="${pageContext.request.contextPath}/Logout">注销</a></li>
</ul>
</div>
</div>
......
......@@ -43,6 +43,7 @@
<button type="button" class="btn btn-default" @click="login()">登录</button>
<button type="button" class="btn btn-default" @click="register()">注册</button>
</form>
<p id="hint" style="color: red"></p>
</div>
<script>
var loginVue=new Vue({
......@@ -62,11 +63,24 @@
{emulateJSON: true}
).then(function (value) {
location.reload();
},function (reason) {
document.getElementById("hint").innerText="用户名或密码不正确";
});
},
register:function () {
console.log(this.user.name);
console.log(this.user.password);
this.$http.post("${pageContext.request.contextPath}/Register",
{
name:this.user.name,
password:this.user.password
},
{emulateJSON: true}
).then(function (value) {
location.reload();
},function (reason) {
document.getElementById("hint").innerText="用户名已存在";
});
}
}
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment