Skip to content

Commit c1765ee

Browse files
committed
Spring Boot Shiro Remember Me
1 parent 54f67b0 commit c1765ee

File tree

16 files changed

+705
-0
lines changed

16 files changed

+705
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.springboot</groupId>
7+
<artifactId>Spring-Boot-Shiro-RememberMe</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>demo</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.9.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.7</java.version>
25+
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
26+
<thymeleaf-layout-dialect.version>2.0.1</thymeleaf-layout-dialect.version>
27+
</properties>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.springframework.boot</groupId>
32+
<artifactId>spring-boot-starter-web</artifactId>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>org.mybatis.spring.boot</groupId>
37+
<artifactId>mybatis-spring-boot-starter</artifactId>
38+
<version>1.3.1</version>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
44+
</dependency>
45+
46+
<!-- shiro-spring -->
47+
<dependency>
48+
<groupId>org.apache.shiro</groupId>
49+
<artifactId>shiro-spring</artifactId>
50+
<version>1.4.0</version>
51+
</dependency>
52+
53+
54+
<!-- oracle驱动 -->
55+
<dependency>
56+
<groupId>com.oracle</groupId>
57+
<artifactId>ojdbc6</artifactId>
58+
<version>6.0</version>
59+
</dependency>
60+
61+
<!-- druid数据源驱动 -->
62+
<dependency>
63+
<groupId>com.alibaba</groupId>
64+
<artifactId>druid-spring-boot-starter</artifactId>
65+
<version>1.1.6</version>
66+
</dependency>
67+
68+
</dependencies>
69+
70+
<build>
71+
<plugins>
72+
<plugin>
73+
<groupId>org.springframework.boot</groupId>
74+
<artifactId>spring-boot-maven-plugin</artifactId>
75+
</plugin>
76+
</plugins>
77+
</build>
78+
79+
80+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.springboot;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
7+
@SpringBootApplication
8+
public class Application {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(Application.class,args);
12+
}
13+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.springboot.config;
2+
3+
import java.util.LinkedHashMap;
4+
5+
import org.apache.shiro.codec.Base64;
6+
import org.apache.shiro.mgt.SecurityManager;
7+
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
8+
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
9+
import org.apache.shiro.web.mgt.CookieRememberMeManager;
10+
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
11+
import org.apache.shiro.web.servlet.SimpleCookie;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
15+
import com.springboot.shiro.ShiroRealm;
16+
17+
@Configuration
18+
public class ShiroConfig {
19+
20+
@Bean
21+
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
22+
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
23+
shiroFilterFactoryBean.setSecurityManager(securityManager);
24+
shiroFilterFactoryBean.setLoginUrl("/login");
25+
shiroFilterFactoryBean.setSuccessUrl("/index");
26+
shiroFilterFactoryBean.setUnauthorizedUrl("/403");
27+
28+
LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
29+
30+
filterChainDefinitionMap.put("/css/**", "anon");
31+
filterChainDefinitionMap.put("/js/**", "anon");
32+
filterChainDefinitionMap.put("/fonts/**", "anon");
33+
filterChainDefinitionMap.put("/img/**", "anon");
34+
filterChainDefinitionMap.put("/druid/**", "anon");
35+
filterChainDefinitionMap.put("/logout", "logout");
36+
filterChainDefinitionMap.put("/", "anon");
37+
filterChainDefinitionMap.put("/**", "user");
38+
39+
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
40+
41+
return shiroFilterFactoryBean;
42+
}
43+
44+
@Bean
45+
public SecurityManager securityManager(){
46+
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
47+
securityManager.setRealm(shiroRealm());
48+
securityManager.setRememberMeManager(rememberMeManager());
49+
return securityManager;
50+
}
51+
52+
@Bean(name = "lifecycleBeanPostProcessor")
53+
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
54+
return new LifecycleBeanPostProcessor();
55+
}
56+
57+
@Bean
58+
public ShiroRealm shiroRealm(){
59+
ShiroRealm shiroRealm = new ShiroRealm();
60+
return shiroRealm;
61+
}
62+
63+
/**
64+
* cookie对象
65+
* @return
66+
*/
67+
public SimpleCookie rememberMeCookie() {
68+
// 设置cookie名称,对应login.html页面的<input type="checkbox" name="rememberMe"/>
69+
SimpleCookie cookie = new SimpleCookie("rememberMe");
70+
// 设置cookie的过期时间,单位为秒,这里为一天
71+
cookie.setMaxAge(86400);
72+
return cookie;
73+
}
74+
75+
/**
76+
* cookie管理对象
77+
* @return
78+
*/
79+
public CookieRememberMeManager rememberMeManager() {
80+
CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
81+
cookieRememberMeManager.setCookie(rememberMeCookie());
82+
// rememberMe cookie加密的密钥
83+
cookieRememberMeManager.setCipherKey(Base64.decode("3AvVhmFLUs0KTA3Kprsdag=="));
84+
return cookieRememberMeManager;
85+
}
86+
87+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.springboot.controller;
2+
3+
import org.apache.shiro.SecurityUtils;
4+
import org.apache.shiro.authc.AuthenticationException;
5+
import org.apache.shiro.authc.IncorrectCredentialsException;
6+
import org.apache.shiro.authc.LockedAccountException;
7+
import org.apache.shiro.authc.UnknownAccountException;
8+
import org.apache.shiro.authc.UsernamePasswordToken;
9+
import org.apache.shiro.subject.Subject;
10+
import org.springframework.stereotype.Controller;
11+
import org.springframework.ui.Model;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PostMapping;
14+
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.ResponseBody;
16+
17+
import com.springboot.pojo.ResponseBo;
18+
import com.springboot.pojo.User;
19+
import com.springboot.util.MD5Utils;
20+
21+
@Controller
22+
public class LoginController {
23+
24+
@GetMapping("/login")
25+
public String login() {
26+
return "login";
27+
}
28+
29+
@PostMapping("/login")
30+
@ResponseBody
31+
public ResponseBo login(String username, String password, Boolean rememberMe) {
32+
password = MD5Utils.encrypt(username, password);
33+
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
34+
Subject subject = SecurityUtils.getSubject();
35+
try {
36+
subject.login(token);
37+
return ResponseBo.ok();
38+
} catch (UnknownAccountException e) {
39+
return ResponseBo.error(e.getMessage());
40+
} catch (IncorrectCredentialsException e) {
41+
return ResponseBo.error(e.getMessage());
42+
} catch (LockedAccountException e) {
43+
return ResponseBo.error(e.getMessage());
44+
} catch (AuthenticationException e) {
45+
return ResponseBo.error("认证失败!");
46+
}
47+
}
48+
49+
@RequestMapping("/")
50+
public String redirectIndex() {
51+
return "redirect:/index";
52+
}
53+
54+
@RequestMapping("/index")
55+
public String index(Model model) {
56+
User user = (User) SecurityUtils.getSubject().getPrincipal();
57+
model.addAttribute("user", user);
58+
return "index";
59+
}
60+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.springboot.dao;
2+
3+
import org.apache.ibatis.annotations.Mapper;
4+
5+
import com.springboot.pojo.User;
6+
7+
@Mapper
8+
public interface UserMapper {
9+
User findByUserName(String userName);
10+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.springboot.pojo;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class ResponseBo extends HashMap<String, Object>{
7+
private static final long serialVersionUID = 1L;
8+
9+
public ResponseBo() {
10+
put("code", 0);
11+
put("msg", "操作成功");
12+
}
13+
14+
public static ResponseBo error() {
15+
return error(1, "操作失败");
16+
}
17+
18+
public static ResponseBo error(String msg) {
19+
return error(500, msg);
20+
}
21+
22+
public static ResponseBo error(int code, String msg) {
23+
ResponseBo ResponseBo = new ResponseBo();
24+
ResponseBo.put("code", code);
25+
ResponseBo.put("msg", msg);
26+
return ResponseBo;
27+
}
28+
29+
public static ResponseBo ok(String msg) {
30+
ResponseBo ResponseBo = new ResponseBo();
31+
ResponseBo.put("msg", msg);
32+
return ResponseBo;
33+
}
34+
35+
public static ResponseBo ok(Map<String, Object> map) {
36+
ResponseBo ResponseBo = new ResponseBo();
37+
ResponseBo.putAll(map);
38+
return ResponseBo;
39+
}
40+
41+
public static ResponseBo ok() {
42+
return new ResponseBo();
43+
}
44+
45+
@Override
46+
public ResponseBo put(String key, Object value) {
47+
super.put(key, value);
48+
return this;
49+
}
50+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.springboot.pojo;
2+
3+
import java.io.Serializable;
4+
import java.util.Date;
5+
6+
public class User implements Serializable{
7+
8+
private static final long serialVersionUID = -5440372534300871944L;
9+
10+
private Integer id;
11+
private String userName;
12+
private String password;
13+
private Date createTime;
14+
private String status;
15+
public Integer getId() {
16+
return id;
17+
}
18+
public void setId(Integer id) {
19+
this.id = id;
20+
}
21+
public String getUserName() {
22+
return userName;
23+
}
24+
public void setUserName(String userName) {
25+
this.userName = userName;
26+
}
27+
public String getPassword() {
28+
return password;
29+
}
30+
public void setPassword(String password) {
31+
this.password = password;
32+
}
33+
public Date getCreateTime() {
34+
return createTime;
35+
}
36+
public void setCreateTime(Date createTime) {
37+
this.createTime = createTime;
38+
}
39+
public String getStatus() {
40+
return status;
41+
}
42+
public void setStatus(String status) {
43+
this.status = status;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)