生活随笔
收集整理的這篇文章主要介紹了
SpringBoot集成Spring Security(二)注册 、密码加密、修改密码
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
SpringBoot集成Spring Security(一)登錄注銷
寫在前面
上一節(jié)創(chuàng)建了項(xiàng)目并且利用Spring Security完成了登錄注銷功能,這里繼續(xù)說一下注冊、密碼加密和找回密碼,代碼注釋較清晰。
一、web層
控制
StudentController.java
package com
.jxnu
.os
.controller
;import com
.jxnu
.os
.model
.RespBean
;
import com
.jxnu
.os
.model
.Student
;
import com
.jxnu
.os
.service
.StudentService
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.web
.bind
.annotation
.*
;import java
.util
.List
;
@RestController
@RequestMapping("/student")
public class StudentController {@AutowiredStudentService studentService
;@PostMapping("/register")public RespBean
register(Student student
) {if(studentService
.register(student
)){return RespBean
.ok("注冊成功",student
);}else{return RespBean
.error("注冊失敗,用戶名已存在");}}@PutMapping("/modifyPass")public RespBean
modifyPass(String sno
,String password
,String rePassword
) {if(studentService
.modifyPass(sno
,password
,rePassword
)){return RespBean
.ok("修改成功");}else{return RespBean
.error("修改失敗,原密碼錯誤");}}}
二、service層
注冊時對密碼進(jìn)行加密,修改密碼時則需先解密后驗(yàn)證
StudentService.java
package com
.jxnu
.os
.service
;import com
.jxnu
.os
.mapper
.StudentMapper
;
import com
.jxnu
.os
.model
.Student
;
import com
.jxnu
.os
.utils
.StudentUtils
;
import org
.springframework
.beans
.factory
.annotation
.Autowired
;
import org
.springframework
.security
.core
.userdetails
.UserDetails
;
import org
.springframework
.security
.core
.userdetails
.UserDetailsService
;
import org
.springframework
.security
.core
.userdetails
.UsernameNotFoundException
;
import org
.springframework
.security
.crypto
.bcrypt
.BCryptPasswordEncoder
;
import org
.springframework
.stereotype
.Service
;import java
.util
.List
;
@Service
public class StudentService implements UserDetailsService {@AutowiredStudentMapper studentMapper
;public boolean register(Student student
) {Student existUser
= studentMapper
.loadUserBySno(student
.getUsername());if (existUser
!= null
) {return false;} else {BCryptPasswordEncoder encoder
= new BCryptPasswordEncoder();String encode
= encoder
.encode(student
.getPassword());student
.setPassword(encode
);studentMapper
.insert(student
);return true;}}public boolean modifyPass(String sno
,String password
,String rePassword
) {Student student
= studentMapper
.loadUserBySno(sno
);BCryptPasswordEncoder encoder
= new BCryptPasswordEncoder();if(encoder
.matches(password
,student
.getPassword())) {String encode
= encoder
.encode(rePassword
);rePassword
= encode
;studentMapper
.modifyPass(sno
,rePassword
);return true;}else {return false;}}}
三、mapper層
StudentMapper.java
package com
.jxnu
.os
.mapper
;import com
.jxnu
.os
.model
.Student
;
import org
.apache
.ibatis
.annotations
.Param
;import java
.util
.List
;
public interface StudentMapper {int insert(Student student
);int modifyPass(String sno
,String rePassword
);
}
StudentMapper.xml
<?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="com.jxnu.os.mapper.StudentMapper"><resultMap id="BaseResultMap" type="com.jxnu.os.model.Student"><id column="id" property="id" jdbcType="INTEGER"/><result column="username" property="username" jdbcType="VARCHAR"/><result column="sno" property="sno" jdbcType="VARCHAR"/><result column="s_sex" property="s_sex" jdbcType="CHAR"/><result column="t_id" property="t_id" jdbcType="INTEGER"/><result column="password" property="password" jdbcType="VARCHAR"/></resultMap><update id="modifyPass" parameterType="com.jxnu.os.model.Student">update student set password = #{rePassword} where sno=#{sno}
</update><insert id="insert" parameterType="com.jxnu.os.model.Student">insert into student (username,password)values (#{username,jdbcType=VARCHAR},#{password,jdbcType=VARCHAR})
</insert></mapper>
總結(jié)
以上是生活随笔為你收集整理的SpringBoot集成Spring Security(二)注册 、密码加密、修改密码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。