一、创建User实体类
public class User {
private Integer id;
private String username;
private String password;
private String email;
private Date created;
private Integer valid;
public User(String username, String password, String email) {
super();
this.username = username;
this.password = password;
this.email = email;
}
public User() {
super();
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public Integer getValid() {
return valid;
}
public void setValid(Integer valid) {
this.valid = valid;
}
}
二、创建UserMapper数据访问接口
@Mapper
public interface UserMapper {
// 新增用户,同时使用@Options注解获取自动生成的主键id
@Insert("INSERT INTO t_user (username,password,email,created,valid) " +
" VALUES (#{username},#{password}, #{email}, #{created}, #{valid}) ")
@Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
public Integer addUser(User user);
//给新用户分配权限,默认是普通用户,权限id=2
@Insert("INSERT INTO t_user_authority (user_id,authority_id) " +
" VALUES (#{id},2) ")
public Integer giveAuthorityToNewUser(User user) ;
}
三、创建IUserService服务接口
public interface IUserService {
/**
* add user
* @param user
*/
public void addUser(User user);
}
四、创建UserServiceImpl服务实现类
@Service
@Transactional
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
@Override
public void addUser(User user) {
userMapper.addUser(user);
//给新用户分配普通用户权限
userMapper.giveAuthorityToNewUser(user);
}
}
五、修改安全配置SecurityConfig
1、放行"/regist"请求
antMatchers("/","/page/**","/article/**","/login","/regist").permitAll()
2、配置密码编码器
/**
* 重写configure(AuthenticationManagerBuilder auth)方法,进行自定义用户认证
* @param auth
* @throws Exception
*/
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
// 密码需要设置编码器
PasswordEncoder encoder = this.passwordEncoder();
// 使用JDBC进行身份认证
String userSQL ="select username,password,valid from t_user where username = ?";
String authoritySQL ="select u.username,a.authority from t_user u,t_authority a," +
"t_user_authority ua where ua.user_id=u.id " +
"and ua.authority_id=a.id and u.username =?";
auth.jdbcAuthentication().passwordEncoder(encoder)
.dataSource(dataSource)
.usersByUsernameQuery(userSQL)
.authoritiesByUsernameQuery(authoritySQL);
}
/**
* BCryptPasswordEncoder password encoder
* @return
*/
@Description("Standard PasswordEncoder")
@Bean
public PasswordEncoder passwordEncoder(){
return new BCryptPasswordEncoder();
}
六、创建RegistController注册请求处理器
@Controller
public class RegistController {
private static final Logger logger = LoggerFactory.getLogger(RegistController.class);
@Autowired
private IUserService userService;
@Autowired
private PasswordEncoder passwordEncoder;
// 向注册页面跳转
@GetMapping(value = "/regist")
public String toRegist() {
return "comm/regist";
}
// 注册
@PostMapping(value = "/regist")
public String regist(HttpServletRequest request,
@RequestParam String username, @RequestParam String password,@RequestParam String email) {
// 封装用户信息
password=passwordEncoder.encode(password);
User user= new User(username,password,email);
user.setCreated(new Date());
user.setValid(1);
try {
userService.addUser(user);
logger.info("新增用户成功,对应用户id: "+user.getId());
return "redirect:/";
} catch (Exception e) {
logger.error("新增用户失败"+"错误描述: "+e.getMessage());
return "redirect:/regist?error";
}
}
}
七、创建注册页regist.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>注册</title>
<meta http-equiv="Cache-Control" content="no-siteapp"/>
<link rel="shortcut icon" th:href="@{/user/img/bloglogo.jpg}"/>
<script th:src="@{/assets/js/jquery.min.js}"></script>
<script th:src="@{/assets/js/amazeui.min.js}"></script>
<link rel="stylesheet" th:href="@{/assets/css/amazeui.min.css}"/>
<link rel="stylesheet" th:href="@{/assets/css/app.css}"/>
</head>
<body>
<div class="log">
<div class="am-g">
<div class="am-u-lg-3 am-u-md-6 am-u-sm-8 am-u-sm-centered log-content">
<h1 class="log-title am-animation-slide-top" style="color: black;" th:text="#{regist.welcomeTitle}">~欢迎注册~</h1>
<br>
<div th:if="${param.error}" style="color: red" th:text="#{regist.error}">有错误!</div>
<form class="am-form" id="registForm" th:action="@{/regist}" method="post">
<div>
<input type="hidden" name="url" th:value="${url}">
</div>
<div class="am-input-group am-radius am-animation-slide-left">
<input type="text" class="am-radius" th:placeholder="#{login.username}" name="username" />
<span class="am-input-group-label log-icon am-radius">
<i class="am-icon-user am-icon-sm am-icon-fw"></i>
</span>
</div>
<br>
<div class="am-input-group am-animation-slide-left log-animation-delay">
<input type="password" class="am-form-field am-radius log-input" th:placeholder="#{login.password}" name="password" />
<span class="am-input-group-label log-icon am-radius">
<i class="am-icon-lock am-icon-sm am-icon-fw"></i>
</span>
</div>
<br>
<div class="am-input-group am-animation-slide-left log-animation-delay">
<input type="text" class="am-radius" th:placeholder="#{regist.email}" name="email" />
<span class="am-input-group-label log-icon am-radius">
<i class="am-icon-email am-icon-sm am-icon-fw"></i>
</span>
</div>
<div style="padding-top: 10px;">
<input type="submit" th:value="#{regist.sub}"
class="am-btn am-btn-primary am-btn-block am-btn-lg am-radius am-animation-slide-bottom log-animation-delay" />
</div>
</form>
</div>
</div>
<footer class="log-footer">
<p style="margin: 30px; color: #2E2D3C"><time class="comment-time" th:text="${#dates.format(new java.util.Date().getTime(), 'yyyy')}"></time> © Powered By <a style="color: #0e90d2" rel="nofollow">CrazyStone</a></p>
</footer>
</div>
</body>
</html>