1.service层接口实现根据主键查询用户
package com. shsxt. crm. system. service;
import com. shsxt. crm. system. model. UserModel;
import com. shsxt. crm. system. pojo. User;
public interface UserServiceI {
UserModel userLogin ( User user, String rememberMe) ;
void updateUserPwd ( Integer id, String oldPassword, String newPassword, String confirmPassword) ;
User selectUserById ( Integer id) ;
}
2.service层实现类
package com. shsxt. crm. system. service. impl;
import com. shsxt. crm. base. util. AssertUtil;
import com. shsxt. crm. base. util. Base64Util;
import com. shsxt. crm. base. util. Md5Util;
import com. shsxt. crm. system. dao. UserMapper;
import com. shsxt. crm. system. model. UserModel;
import com. shsxt. crm. system. pojo. User;
import com. shsxt. crm. system. service. UserServiceI;
import org. apache. commons. lang3. StringUtils;
import org. springframework. beans. BeanUtils;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Service;
@Service
public class UserServiceImpl implements UserServiceI {
@Autowired
private UserMapper userMapper;
@Override
public UserModel userLogin ( User user, String rememberMe) {
String userName = user. getUserName ( ) ;
String userPwd = user. getUserPwd ( ) ;
AssertUtil. isTrue ( StringUtils. isBlank ( userName) , "用户名不能为空" ) ;
AssertUtil. isTrue ( StringUtils. isBlank ( userPwd) , "密码不能为空" ) ;
User u = userMapper. selectUserByUserName ( user. getUserName ( ) . trim ( ) ) ;
AssertUtil. isTrue ( null == u, "该用户名不存在" ) ;
AssertUtil. isTrue ( ! u. getUserPwd ( ) . equals ( Md5Util. encode ( userPwd) ) , "密码输入错误" ) ;
return buildUserModel ( u, userPwd, rememberMe) ;
}
@Override
public void updateUserPwd ( Integer id, String oldPassword, String newPassword, String confirmPassword) {
AssertUtil. isTrue ( StringUtils. isBlank ( oldPassword) , "原密码不可以为空" ) ;
AssertUtil. isTrue ( StringUtils. isBlank ( newPassword) , "新密码不可以为空" ) ;
AssertUtil. isTrue ( ! newPassword. equals ( confirmPassword) , "两次密码不一致" ) ;
User user = userMapper. selectByPrimaryKey ( id) ;
AssertUtil. isTrue ( null == user, "用户未登录或不存在" ) ;
AssertUtil. isTrue ( ! user. getUserPwd ( ) . equals ( Md5Util. encode ( oldPassword) ) , "原密码输入错误" ) ;
user. setUserPwd ( Md5Util. encode ( newPassword) ) ;
AssertUtil. isTrue ( userMapper. updateByPrimaryKeySelective ( user) < 1 , "修改密码失败" ) ;
}
@Override
public User selectUserById ( Integer id) {
return userMapper. selectByPrimaryKey ( id) ;
}
private UserModel buildUserModel ( User user, String userPwd, String rememberMe) {
UserModel userModel = new UserModel ( ) ;
BeanUtils. copyProperties ( user, userModel) ;
userModel. setUserIdStr ( Base64Util. encoder ( String. valueOf ( user. getId ( ) ) ) ) ;
if ( StringUtils. isNotBlank ( rememberMe) )
userModel. setUserInfo ( Base64Util. encoder ( userPwd) ) ;
return userModel;
}
}
3.建立登录异常类
package com. shsxt. crm. base. exception;
import com. shsxt. crm. base. constants. CrmConstants;
public class LoginException extends RuntimeException {
private Integer code = CrmConstants. OPS_FAILED_CODE;
private String message = "用户未登录或不存在" ;
public LoginException ( ) {
}
public LoginException ( Integer code, String message) {
super ( message) ;
this . code = code;
this . message = message;
}
public LoginException ( Integer code) {
super ( "用户未登录或不存在" ) ;
this . code = code;
}
public LoginException ( String message) {
super ( message) ;
this . message = message;
}
public Integer getCode ( ) {
return code;
}
public void setCode ( Integer code) {
this . code = code;
}
public String getMessage ( ) {
return message;
}
public void setMessage ( String message) {
this . message = message;
}
}
4.修改断言工具类
package com. shsxt. crm. base. util;
import com. shsxt. crm. base. exception. LoginException;
import com. shsxt. crm. base. exception. ParamsException;
public class AssertUtil {
public static void isTrue ( boolean flag) {
if ( flag)
throw new ParamsException ( ) ;
}
public static void isTrue ( boolean flag, String message) {
if ( flag)
throw new ParamsException ( message) ;
}
public static void isTrue ( boolean flag, Integer code, String message) {
if ( flag)
throw new ParamsException ( code, message) ;
}
public static void isNotLogin ( boolean flag) {
if ( flag)
throw new LoginException ( ) ;
}
public static void isNotLogin ( boolean flag, String message) {
if ( flag)
throw new LoginException ( message) ;
}
public static void isNotLogin ( boolean flag, Integer code, String message) {
if ( flag)
throw new LoginException ( code, message) ;
}
}
5.建立登录拦截器
package com. shsxt. crm. base. interceptor;
import com. shsxt. crm. base. util. AssertUtil;
import com. shsxt. crm. base. util. Base64Util;
import com. shsxt. crm. base. util. CookieUtil;
import com. shsxt. crm. system. pojo. User;
import com. shsxt. crm. system. service. UserServiceI;
import org. apache. commons. lang3. StringUtils;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. web. servlet. handler. HandlerInterceptorAdapter;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
public class LoginInterceptor extends HandlerInterceptorAdapter {
@Autowired
private UserServiceI userServiceI;
@Override
public boolean preHandle ( HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
String userIdStr= CookieUtil. getCookieValue ( request, "userIdStr" ) ;
AssertUtil. isNotLogin ( StringUtils. isBlank ( userIdStr) ) ;
User user= userServiceI. selectUserById ( Integer. valueOf ( Base64Util. decoder ( userIdStr) ) ) ;
AssertUtil. isNotLogin ( null== user) ;
return true ;
}
}
6.配置拦截器springmvc
<?xml version="1.0" encoding="UTF-8"?>
< beans xmlns = " http://www.springframework.org/schema/beans"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xmlns: mvc= " http://www.springframework.org/schema/mvc"
xmlns: context= " http://www.springframework.org/schema/context"
xsi: schemaLocation= "
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd" >
< mvc: annotation-driven/>
< context: component-scan base-package = " com.shsxt" />
< mvc: resources location = " /WEB-INF/static/" mapping = " /**" />
< bean id = " freemarkerConfig"
class = " org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer" >
< property name = " templateLoaderPath" value = " /WEB-INF/views/" />
< property name = " defaultEncoding" value = " UTF-8" />
</ bean>
< bean class = " org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver" >
< property name = " prefix" value = " " />
< property name = " suffix" value = " .ftl" />
< property name = " contentType" value = " text/html;charset=UTF-8" />
</ bean>
< mvc: interceptors>
< mvc: interceptor>
< mvc: mapping path = " /**" />
< mvc: exclude-mapping path = " /login" />
< mvc: exclude-mapping path = " /user/login" />
< mvc: exclude-mapping path = " /user/logout" />
< mvc: exclude-mapping path = " /css" />
< mvc: exclude-mapping path = " /js" />
< mvc: exclude-mapping path = " /images" />
< mvc: exclude-mapping path = " /jquery-easyui-1.3.3" />
< bean class = " com.shsxt.crm.base.interceptor.LoginInterceptor" />
</ mvc: interceptor>
</ mvc: interceptors>
</ beans>
7.重构 BaseResult.java
package com. shsxt. crm. base;
import com. shsxt. crm. base. constants. CrmConstants;
import java. io. Serializable;
public class BaseResult implements Serializable {
private Integer code ;
private String message ;
private Object result;
public static BaseResult success ( ) {
return new BaseResult ( CrmConstants. OPS_SUCCESS_CODE,
CrmConstants. OPS_SUCCESS_MESSAGE) ;
}
public static BaseResult success ( String message) {
return new BaseResult ( CrmConstants. OPS_SUCCESS_CODE, message) ;
}
public static BaseResult success ( String message, Object result) {
return new BaseResult ( CrmConstants. OPS_SUCCESS_CODE, message, result) ;
}
public static BaseResult error ( ) {
return new BaseResult ( CrmConstants. OPS_FAILED_CODE,
CrmConstants. OPS_FAILED_MESSAGE) ;
}
public static BaseResult error ( String message) {
return new BaseResult ( CrmConstants. OPS_FAILED_CODE, message) ;
}
public static BaseResult error ( String message, Object result) {
return new BaseResult ( CrmConstants. OPS_FAILED_CODE, message, result) ;
}
public BaseResult ( ) {
}
public BaseResult ( String message) {
this . message = message;
}
public BaseResult ( Integer code, String message) {
this . code = code;
this . message = message;
}
public BaseResult ( Integer code, Object result) {
this . code = code;
this . result = result;
}
public BaseResult ( String message, Object result) {
this . message = message;
this . result = result;
}
public BaseResult ( Integer code, String message, Object result) {
this . code = code;
this . message = message;
this . result = result;
}
public Integer getCode ( ) {
return code;
}
public void setCode ( Integer code) {
this . code = code;
}
public String getMessage ( ) {
return message;
}
public void setMessage ( String message) {
this . message = message;
}
public Object getResult ( ) {
return result;
}
public void setResult ( Object result) {
this . result = result;
}
}
8.构建 BaseController.java
package com. shsxt. crm. base;
import org. springframework. web. bind. annotation. ModelAttribute;
import javax. servlet. http. HttpServletRequest;
public class BaseController {
@ModelAttribute
public void preMethod ( HttpServletRequest request) {
request. setAttribute ( "ctx" , request. getContextPath ( ) ) ;
}
}
9.重构 Controller.java
package com. shsxt. crm. system. controller;
import com. shsxt. crm. base. BaseController;
import com. shsxt. crm. base. BaseResult;
import com. shsxt. crm. base. util. Base64Util;
import com. shsxt. crm. base. util. CookieUtil;
import com. shsxt. crm. system. model. UserModel;
import com. shsxt. crm. system. pojo. User;
import com. shsxt. crm. system. service. UserServiceI;
import org. springframework. beans. factory. annotation. Autowired;
import org. springframework. stereotype. Controller;
import org. springframework. web. bind. annotation. RequestMapping;
import org. springframework. web. bind. annotation. ResponseBody;
import javax. servlet. http. HttpServletRequest;
@Controller
@RequestMapping ( "/user" )
public class UserController extends BaseController {
@Autowired
private UserServiceI userServiceI;
@RequestMapping ( "/login" )
@ResponseBody
public BaseResult userLogin ( HttpServletRequest request, User user, String rememberMe) {
UserModel userModel = userServiceI. userLogin ( user, rememberMe) ;
request. getSession ( ) . setAttribute ( "user" , userModel) ;
return BaseResult. success ( "登录成功" , userModel) ;
}
@RequestMapping ( "/logout" )
public String userLogout ( HttpServletRequest request) {
request. getSession ( ) . removeAttribute ( "user" ) ;
return "login" ;
}
@RequestMapping ( "/updateUserPwd" )
@ResponseBody
public BaseResult updateUserPwd ( HttpServletRequest request, String oldPassword,
String newPassword, String confirmPassword) {
String userIdStr = CookieUtil. getCookieValue ( request, "userIdStr" ) ;
Integer id = Integer. valueOf ( Base64Util. decoder ( userIdStr) ) ;
userServiceI. updateUserPwd ( id, oldPassword, newPassword, confirmPassword) ;
return BaseResult. success ( "修改成功" ) ;
}
}
package com. shsxt. crm. system. controller;
import com. shsxt. crm. base. BaseController;
import org. springframework. stereotype. Controller;
import org. springframework. web. bind. annotation. RequestMapping;
import javax. servlet. http. HttpServletRequest;
@Controller
public class pageController extends BaseController {
@RequestMapping ( "/login" )
public String login ( ) {
return "login" ;
}
@RequestMapping ( "/index" )
public String index ( ) {
return "index" ;
}
}
10,修改全局异常类
package com. shsxt. crm. base. exception;
import com. alibaba. fastjson. JSON;
import com. shsxt. crm. base. BaseResult;
import com. shsxt. crm. base. constants. CrmConstants;
import org. springframework. stereotype. Component;
import org. springframework. web. bind. annotation. ResponseBody;
import org. springframework. web. method. HandlerMethod;
import org. springframework. web. servlet. HandlerExceptionResolver;
import org. springframework. web. servlet. ModelAndView;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import java. io. IOException;
import java. io. PrintWriter;
@Component
public class GlobalExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException ( HttpServletRequest request,
HttpServletResponse response,
Object handler, Exception e) {
ModelAndView mv = buildModelAndView ( request, e) ;
if ( ! ( handler instanceof HandlerMethod ) )
return mv;
HandlerMethod method = ( HandlerMethod) handler;
ResponseBody responseBody = method. getMethodAnnotation ( ResponseBody. class ) ;
if ( null != responseBody) {
BaseResult baseResult = new BaseResult ( CrmConstants. OPS_FAILED_CODE,
CrmConstants. OPS_FAILED_MESSAGE) ;
if ( e instanceof ParamsException ) {
ParamsException pe = ( ParamsException) e;
baseResult. setCode ( pe. getCode ( ) ) ;
baseResult. setMessage ( pe. getMessage ( ) ) ;
}
if ( e instanceof LoginException ) {
LoginException le = ( LoginException) e;
baseResult. setCode ( le. getCode ( ) ) ;
baseResult. setMessage ( le. getMessage ( ) ) ;
}
response. setCharacterEncoding ( "utf-8" ) ;
response. setContentType ( "application/json;charset=utf-8" ) ;
PrintWriter pw = null;
try {
pw = response. getWriter ( ) ;
pw. write ( JSON. toJSONString ( baseResult) ) ;
pw. flush ( ) ;
} catch ( IOException ex) {
ex. printStackTrace ( ) ;
} finally {
if ( null != pw)
pw. close ( ) ;
}
return null;
} else {
if ( e instanceof ParamsException ) {
ParamsException pe = ( ParamsException) e;
mv. addObject ( "errorMsg" , pe. getMessage ( ) ) ;
mv. addObject ( "errorCode" , pe. getCode ( ) ) ;
}
if ( e instanceof LoginException ) {
LoginException le = ( LoginException) e;
mv. addObject ( "errorMsg" , le. getMessage ( ) ) ;
mv. addObject ( "errorCode" , le. getCode ( ) ) ;
}
}
return mv;
}
private ModelAndView buildModelAndView ( HttpServletRequest request, Exception e) {
request. setAttribute ( "ctx" , request. getContextPath ( ) ) ;
request. setAttribute ( "uri" , request. getRequestURI ( ) ) ;
ModelAndView mv = new ModelAndView ( ) ;
mv. setViewName ( "error" ) ;
mv. addObject ( "errorMsg" , CrmConstants. OPS_FAILED_MESSAGE) ;
mv. addObject ( "errorCode" , CrmConstants. OPS_FAILED_CODE) ;
return mv;
}
}