shiro登录、拦截测试成功
This commit is contained in:
8
sql.sql
8
sql.sql
@@ -68,4 +68,10 @@ INSERT INTO permission_role (rid, pid) VALUE ('3','4');
|
||||
INSERT INTO user_role (rid, uid) VALUE ('1','1');
|
||||
INSERT INTO user_role (rid, uid) VALUE ('2','2');
|
||||
|
||||
|
||||
SELECT u.*,r.*,p.*
|
||||
FROM user u
|
||||
INNER JOIN user_role ur ON ur.uid = u.uid
|
||||
INNER JOIN role r ON ur.uid = r.rid
|
||||
INNER JOIN permission_role pr ON r.rid = pr.rid
|
||||
INNER JOIN permission p ON pr.pid = p.pid
|
||||
WHERE u.username
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package top.fjy8018.shiro.constant;
|
||||
|
||||
/**
|
||||
* 存储全局变量
|
||||
*/
|
||||
public class GlobalConstant {
|
||||
public static final String USER_SESSION_KEY = "USER_SESSION";
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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";
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@ public class Role {
|
||||
|
||||
private Integer rid;
|
||||
|
||||
private String name;
|
||||
private String rname;
|
||||
|
||||
private Set<Permission> permissions = new HashSet<>();
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ public class User {
|
||||
|
||||
private Integer uid;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private Set<Role> roles = new HashSet<>();
|
||||
|
||||
16
src/main/java/top/fjy8018/shiro/form/LoginForm.java
Normal file
16
src/main/java/top/fjy8018/shiro/form/LoginForm.java
Normal 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;
|
||||
}
|
||||
@@ -2,10 +2,15 @@ spring:
|
||||
datasource:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/db_imis2?useUnicode=true&characterEncoding=utf-8&useSSL=true
|
||||
url: jdbc:mysql://localhost:3306/db_shiroTest?useUnicode=true&characterEncoding=utf-8&useSSL=true
|
||||
username: trs
|
||||
password: shiro
|
||||
mvc:
|
||||
## 配置jsp页面位置 ##
|
||||
view:
|
||||
prefix: /pages/
|
||||
suffix: .jsp
|
||||
## 配置mybatis ##
|
||||
mybatis:
|
||||
mapper-locations: mappers/*.xml
|
||||
type-aliases-package: top.fjy8018.shiro.dataobject
|
||||
mapper-locations: mapper/*.xml
|
||||
type-aliases-package: top.fjy8018.shiro.dataobject
|
||||
@@ -9,15 +9,21 @@
|
||||
<collection property="roles" ofType="top.fjy8018.shiro.dataobject.Role">
|
||||
<id property="rid" column="rid" />
|
||||
<result property="rname" column="rname" />
|
||||
<collection property="permission" ofType="top.fjy8018.shiro.dataobject.Permission">
|
||||
<collection property="permissions" ofType="top.fjy8018.shiro.dataobject.Permission">
|
||||
<id property="pid" column="pid" />
|
||||
<result property="name" column="name" />
|
||||
<result property="url" column="url" />
|
||||
</collection>
|
||||
</collection>
|
||||
</resultMap>
|
||||
|
||||
<select id="findByUsername" parameterType="string" resultMap="userMap">
|
||||
|
||||
<select id="findByUsername" parameterType="java.lang.String" resultMap="userMap">
|
||||
SELECT u.*,r.*,p.*
|
||||
FROM user u
|
||||
INNER JOIN user_role ur ON ur.uid = u.uid
|
||||
INNER JOIN role r ON ur.uid = r.rid
|
||||
INNER JOIN permission_role pr ON r.rid = pr.rid
|
||||
INNER JOIN permission p ON pr.pid = p.pid
|
||||
WHERE u.username = #{username}
|
||||
</select>
|
||||
</mapper>
|
||||
16
src/main/webapp/pages/index.jsp
Normal file
16
src/main/webapp/pages/index.jsp
Normal file
@@ -0,0 +1,16 @@
|
||||
<%--
|
||||
Created by IntelliJ IDEA.
|
||||
User: F嘉阳
|
||||
Date: 2018/7/6
|
||||
Time: 11:17
|
||||
To change this template use File | Settings | File Templates.
|
||||
--%>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>home</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>欢迎登录, ${USER_SESSION.username} </h1>
|
||||
</body>
|
||||
</html>
|
||||
21
src/main/webapp/pages/login.jsp
Normal file
21
src/main/webapp/pages/login.jsp
Normal file
@@ -0,0 +1,21 @@
|
||||
<%--
|
||||
Created by IntelliJ IDEA.
|
||||
User: F嘉阳
|
||||
Date: 2018/7/6
|
||||
Time: 11:13
|
||||
To change this template use File | Settings | File Templates.
|
||||
--%>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>login</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>shiro登录测试</h1>
|
||||
<form action="/doLogin" method="post">
|
||||
<input type="text" name="username"><br>
|
||||
<input type="password" name="password"><br>
|
||||
<input type="submit" value="提交">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user