Merge remote-tracking branch 'origin/master'

master
bseayin 2019-08-24 21:00:29 +08:00
commit 47b4051a16
8 changed files with 140 additions and 1 deletions

View File

@ -32,6 +32,7 @@ public class ShiroConfig {
filterChainDefinitionMap.put("/img/**", "anon");
filterChainDefinitionMap.put("/druid/**", "anon");
filterChainDefinitionMap.put("/logout", "logout");
filterChainDefinitionMap.put("/user/register", "anon");
filterChainDefinitionMap.put("/", "anon");
filterChainDefinitionMap.put("/**", "user");
@ -76,6 +77,7 @@ public class ShiroConfig {
* @return
*/
public CookieRememberMeManager rememberMeManager() {
//Cookie 数据存在客户端的浏览器
CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
cookieRememberMeManager.setCookie(rememberMeCookie());
// rememberMe cookie加密的密钥

View File

@ -48,6 +48,8 @@ public class ShiroRealm extends AuthorizingRealm {
if (user == null) {
throw new UnknownAccountException("用户名错误!");
}
//1. MD5加密不可以破解
//2. 登录比较的是,两个密文
if (!password.equals(user.getPwd())) {
throw new IncorrectCredentialsException("密码错误!");
}

View File

@ -30,6 +30,7 @@ public class LoginController {
@PostMapping("/login")
@ResponseBody
public ResponseBo login(String username, String password, Boolean rememberMe) {
password = MD5Utils.encrypt(username, password);
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
Subject subject = SecurityUtils.getSubject();

View File

@ -0,0 +1,65 @@
package com.zz.controller;
import javax.annotation.Resource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zz.entity.User;
import com.zz.pojo.ResponseBo;
import com.zz.service.UserService;
import com.zz.util.KeyUtil;
import com.zz.util.MD5Utils;
@Controller
@RequestMapping("user")
public class UserController {
@Resource
UserService userService;
@GetMapping("/register")
public String login() {
return "register.html";
}
@PostMapping("/register")
@ResponseBody
public ResponseBo register(User user) {
String password = MD5Utils.encrypt(user.getName(), user.getPwd());
user.setPwd(password);
user.setId(KeyUtil.genUniqueKey());
userService.save(user);
return ResponseBo.ok();
}
@RequestMapping("/")
public String redirectIndex() {
return "redirect:/index";
}
@RequestMapping("/index")
public String index(Model model) {
User user = (User) SecurityUtils.getSubject().getPrincipal();
model.addAttribute("user", user);
return "index1.html";
}
@PostMapping("/getlogin")
@ResponseBody
public User getLoginUser(){
return (User) SecurityUtils.getSubject().getPrincipal();
}
}

View File

@ -21,6 +21,11 @@ public class UserService {
};
public User save(User user){
return userRepository.save(user);
};
public Page<User> findAll(String page, String limit){
Pageable pageable = PageRequest.of(Integer.parseInt(page), Integer.parseInt(limit));
Page<User> pageinfo=userRepository.findAll(pageable);

View File

@ -0,0 +1,16 @@
package com.zz.util;
import java.util.Random;
public class KeyUtil {
/**
*
* : +
* @return
*/
public static String genUniqueKey() {
Random random = new Random();
Integer number = random.nextInt(900000) + 100000;
return System.currentTimeMillis() + String.valueOf(number);
}
}

View File

@ -4,7 +4,7 @@ import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
public class MD5Utils {
private static final String SALT = "mrbird";
private static final String SALT = "zzjava10";
private static final String ALGORITH_NAME = "md5";
@ -16,6 +16,7 @@ public class MD5Utils {
}
public static String encrypt(String username, String pswd) {
//加盐密码
String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(username + SALT),
HASH_ITERATIONS).toHex();
return newPassword;

View File

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html>
<head>
<title>Bootstrap 实例</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/css/bootstrap.min.css">
<script src="https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/popper.js/1.12.5/umd/popper.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/4.1.0/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>堆叠表单</h2>
<form>
<div class="form-group">
<label for="email">Email:</label>
<input type="email" class="form-control" id="email" placeholder="Enter email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input type="password" class="form-control" id="pwd" placeholder="Enter password">
</div>
<div class="form-check">
<label class="form-check-label">
<input class="form-check-input" name='rememberMe' type="checkbox"> Remember me
</label>
</div>
<button type="button" id="subtn" class="btn btn-primary">Submit</button>
</form>
</div>
<script type="text/javascript">
$("#subtn").click(function(){
$.post("/r/user/register", { "name": $("#email").val(),"pwd": $("#pwd").val()},
function(data){
console.log(data); //
window.location.href="index1.html";
}, "json");
});
</script>
</body>
</html>