使用layui+ssm完成登录注册修改密码和退出登录
使用ssm完成登录注册
Controller层
package cn. kgc. ssm. controller ;
import cn. hutool. captcha. LineCaptcha ;
import cn. hutool. crypto. SecureUtil ;
import cn. kgc. ssm. entity. Vip ;
import cn. kgc. ssm. service. pjxLoginService ;
import cn. kgc. ssm. util. LayUtil ;
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. ServletOutputStream ;
import javax. servlet. http. HttpServletResponse ;
import javax. servlet. http. HttpSession ;
import java. io. IOException ;
import java. util. HashMap ;
import java. util. Map ;
@Controller
@RequestMapping ( "user" )
public class GUserController {
@Autowired
private pjxLoginService pjxLoginService1;
@RequestMapping ( "captcha" )
public void Captcha ( HttpSession session, HttpServletResponse resp) {
LineCaptcha lineCaptcha = new LineCaptcha ( 260 , 100 , 4 , 5 ) ;
String code = lineCaptcha. getCode ( ) ;
System . out. println ( "code = " + code) ;
session. setAttribute ( "captcha" , code) ;
ServletOutputStream outputStream = null ;
try {
outputStream = resp. getOutputStream ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
lineCaptcha. write ( outputStream) ;
try {
outputStream. close ( ) ;
} catch ( IOException e) {
e. printStackTrace ( ) ;
}
}
@RequestMapping ( "login" )
@ResponseBody
public Object login ( String username, String password, String code, HttpSession session, HttpServletResponse resp) {
System . out. println ( "登录" ) ;
LayUtil layUtils;
Object captcha = session. getAttribute ( "captcha" ) ;
System . out. println ( "captcha = " + captcha) ;
Vip vip = Vip . builder ( ) . username ( username) . password ( SecureUtil . md5 ( password) ) . build ( ) ;
if ( captcha. equals ( code) ) {
System . out. println ( "1" ) ;
layUtils = pjxLoginService1. selectUser ( vip) ;
if ( layUtils. getData ( ) != null ) {
Vip vip1 = ( Vip ) layUtils. getData ( ) ;
session. setAttribute ( "vip1" , vip1) ;
}
} else {
System . out. println ( "2" ) ;
layUtils = new LayUtil ( ) ;
layUtils. setCode ( 500 ) ;
layUtils. setMsg ( "验证码错误!!" ) ;
}
return layUtils;
}
@ResponseBody
@RequestMapping ( "register" )
public Map register ( String username, String password) {
HashMap < Object , Object > map = new HashMap < > ( ) ;
Vip vip = Vip . builder ( ) . username ( username) . password ( SecureUtil . md5 ( password) ) . build ( ) ;
Vip vip1 = pjxLoginService1. selectByName ( username) ;
if ( vip1 == null ) {
boolean b = pjxLoginService1. addVip ( vip) ;
if ( b) {
map. put ( "code" , 0 ) ;
map. put ( "msg" , "注册成功" ) ;
} else {
map. put ( "code" , 500 ) ;
map. put ( "msg" , "注册失败" ) ;
}
} else {
map. put ( "code" , 500 ) ;
map. put ( "msg" , "用户名已存在" ) ;
}
return map;
}
@RequestMapping ( "resetpassword" )
@ResponseBody
public Map resetpassword ( String oldpwd, String newpwd, HttpSession session) {
Vip vip1 = ( Vip ) session. getAttribute ( "vip1" ) ;
String password = vip1. getPassword ( ) ;
long id = vip1. getId ( ) ;
HashMap < String , Object > map = new HashMap < > ( ) ;
if ( SecureUtil . md5 ( oldpwd) . equals ( password) ) {
boolean b = pjxLoginService1. resetPasswordById ( id, SecureUtil . md5 ( newpwd) ) ;
if ( b) {
map. put ( "code" , 0 ) ;
map. put ( "msg" , "密码修改成功" ) ;
} else {
map. put ( "code" , 500 ) ;
map. put ( "msg" , "密码修改失败" ) ;
}
} else {
map. put ( "code" , 500 ) ;
map. put ( "msg" , "原始密码不正确" ) ;
}
return map;
}
@RequestMapping ( "logout" )
@ResponseBody
public Map logout ( HttpSession session) {
HashMap < String , Object > map = new HashMap < > ( ) ;
try {
session. invalidate ( ) ;
map. put ( "code" , 0 ) ;
map. put ( "msg" , "退出登录成功" ) ;
} catch ( Exception throwables) {
throwables. printStackTrace ( ) ;
map. put ( "code" , 500 ) ;
map. put ( "msg" , "退出登录失败" ) ;
}
return map;
}
}
Service层
package cn. kgc. ssm. service. impl ;
import cn. kgc. ssm. entity. Vip ;
import cn. kgc. ssm. mapper. pjxLoginMapper ;
import cn. kgc. ssm. service. pjxLoginService ;
import cn. kgc. ssm. util. LayUtil ;
import org. springframework. beans. factory. annotation. Autowired ;
import org. springframework. stereotype. Service ;
import javax. servlet. http. HttpSession ;
import java. util. HashMap ;
import java. util. Map ;
@Service
public class pjxLoginServiceImpl implements pjxLoginService {
@Autowired
private pjxLoginMapper pjxLoginMapper1;
@Override
public LayUtil selectUser ( Vip vip) {
System . out. println ( "ser" ) ;
System . out. println ( "vip = " + vip) ;
Vip vip1 = pjxLoginMapper1. selectByUserName ( vip. getUsername ( ) ) ;
System . out. println ( "vip1 = " + vip1) ;
LayUtil layUtils = new LayUtil ( ) ;
if ( vip1 == null ) {
layUtils. setCode ( 500 ) ;
layUtils. setMsg ( "账户不存在" ) ;
} else {
String password1 = vip. getPassword ( ) ;
String password2 = vip1. getPassword ( ) ;
if ( password1. equals ( password2) ) {
layUtils. setCode ( 0 ) ;
layUtils. setData ( vip1) ;
layUtils. setMsg ( "登陆成功" ) ;
} else {
layUtils. setCode ( 500 ) ;
layUtils. setMsg ( "密码错误" ) ;
}
}
return layUtils;
}
@Override
public boolean addVip ( Vip vip) {
int line= pjxLoginMapper1. addVip ( vip) ;
return line> 0 ;
}
@Override
public Vip selectByName ( String username) {
Vip vip = pjxLoginMapper1. selectByUserName ( username) ;
return vip;
}
@Override
public boolean resetPasswordById ( long id, String md5) {
int line= pjxLoginMapper1. resetPassword ( id, md5) ;
return line> 0 ;
}
}
Mapper
< ? xml version= "1.0" encoding= "UTF-8" ? >
< ! DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
< mapper namespace= "cn.kgc.ssm.mapper.pjxLoginMapper" >
< insert id= "addVip" >
insert into vip ( username, password) values ( #{ username} , #{ password} )
< / insert>
< update id= "resetPassword" >
update vip set password= #{ md5} where id= #{ id}
< / update>
< select id= "selectByUserName" resultType= "cn.kgc.ssm.entity.Vip" >
select * from vip where username= #{ username}
< / select>
< select id= "selectOldPwd" resultType= "vip" >
select password from vip where id= #{ id}
< / select>
< / mapper>