shiro登录、拦截测试成功

This commit is contained in:
fjy8018@qq.com
2018-07-06 15:14:36 +08:00
parent f612e68cfc
commit 14bdaf5989
12 changed files with 150 additions and 9 deletions

View File

@@ -7,7 +7,7 @@ import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan
@MapperScan(basePackages = {"top.fjy8018.shiro.mapper.UserMapper"})
@MapperScan(basePackages = {"top.fjy8018.shiro.mapper"})
public class ShiroApplication {
public static void main(String[] args) {

View File

@@ -0,0 +1,8 @@
package top.fjy8018.shiro.constant;
/**
* 存储全局变量
*/
public class GlobalConstant {
public static final String USER_SESSION_KEY = "USER_SESSION";
}

View File

@@ -0,0 +1,43 @@
package top.fjy8018.shiro.controller;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import top.fjy8018.shiro.constant.GlobalConstant;
import top.fjy8018.shiro.dataobject.User;
import top.fjy8018.shiro.form.LoginForm;
import javax.servlet.http.HttpSession;
/**
* @author F嘉阳
* @date 2018/7/6 10:59
*/
@Controller
public class LoginController {
@RequestMapping("/login")
public String login(){
return "login";
}
@RequestMapping("/doLogin")
public String doLogin(LoginForm form, HttpSession session){
UsernamePasswordToken token = new UsernamePasswordToken(form.getUsername(),form.getPassword());
Subject subject = SecurityUtils.getSubject();
try{
subject.login(token);
// 若未发生异常则此处获得用户
User user = (User) subject.getPrincipal();
// 将用户写入session
session.setAttribute(GlobalConstant.USER_SESSION_KEY,user);
return "index";
}catch (Exception e){
e.printStackTrace();
return "login";
}
}
}

View File

@@ -0,0 +1,18 @@
package top.fjy8018.shiro.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* 页面导航
* @author F嘉阳
* @date 2018/7/6 10:57
*/
@Controller
public class NavController {
@RequestMapping("/index")
public String index(){
return "index";
}
}

View File

@@ -10,7 +10,7 @@ public class Role {
private Integer rid;
private String name;
private String rname;
private Set<Permission> permissions = new HashSet<>();

View File

@@ -10,6 +10,8 @@ public class User {
private Integer uid;
private String username;
private String password;
private Set<Role> roles = new HashSet<>();

View File

@@ -0,0 +1,16 @@
package top.fjy8018.shiro.form;
import lombok.Data;
/**
* 登录表单
* @author F嘉阳
* @date 2018/7/6 10:59
*/
@Data
public class LoginForm {
private String username;
private String password;
}