forked from wuyouzhuguli/SpringAll
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54f67b0
commit c1765ee
Showing
16 changed files
with
705 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>com.springboot</groupId> | ||
<artifactId>Spring-Boot-Shiro-RememberMe</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>1.5.9.RELEASE</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> | ||
<java.version>1.7</java.version> | ||
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> | ||
<thymeleaf-layout-dialect.version>2.0.1</thymeleaf-layout-dialect.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.mybatis.spring.boot</groupId> | ||
<artifactId>mybatis-spring-boot-starter</artifactId> | ||
<version>1.3.1</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-thymeleaf</artifactId> | ||
</dependency> | ||
|
||
<!-- shiro-spring --> | ||
<dependency> | ||
<groupId>org.apache.shiro</groupId> | ||
<artifactId>shiro-spring</artifactId> | ||
<version>1.4.0</version> | ||
</dependency> | ||
|
||
|
||
<!-- oracle驱动 --> | ||
<dependency> | ||
<groupId>com.oracle</groupId> | ||
<artifactId>ojdbc6</artifactId> | ||
<version>6.0</version> | ||
</dependency> | ||
|
||
<!-- druid数据源驱动 --> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>druid-spring-boot-starter</artifactId> | ||
<version>1.1.6</version> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
12.Spring-Boot-Shiro-RememberMe/src/main/java/com/springboot/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.springboot; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
|
||
@SpringBootApplication | ||
public class Application { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(Application.class,args); | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
12.Spring-Boot-Shiro-RememberMe/src/main/java/com/springboot/config/ShiroConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package com.springboot.config; | ||
|
||
import java.util.LinkedHashMap; | ||
|
||
import org.apache.shiro.codec.Base64; | ||
import org.apache.shiro.mgt.SecurityManager; | ||
import org.apache.shiro.spring.LifecycleBeanPostProcessor; | ||
import org.apache.shiro.spring.web.ShiroFilterFactoryBean; | ||
import org.apache.shiro.web.mgt.CookieRememberMeManager; | ||
import org.apache.shiro.web.mgt.DefaultWebSecurityManager; | ||
import org.apache.shiro.web.servlet.SimpleCookie; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import com.springboot.shiro.ShiroRealm; | ||
|
||
@Configuration | ||
public class ShiroConfig { | ||
|
||
@Bean | ||
public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) { | ||
ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean(); | ||
shiroFilterFactoryBean.setSecurityManager(securityManager); | ||
shiroFilterFactoryBean.setLoginUrl("/login"); | ||
shiroFilterFactoryBean.setSuccessUrl("/index"); | ||
shiroFilterFactoryBean.setUnauthorizedUrl("/403"); | ||
|
||
LinkedHashMap<String, String> filterChainDefinitionMap = new LinkedHashMap<>(); | ||
|
||
filterChainDefinitionMap.put("/css/**", "anon"); | ||
filterChainDefinitionMap.put("/js/**", "anon"); | ||
filterChainDefinitionMap.put("/fonts/**", "anon"); | ||
filterChainDefinitionMap.put("/img/**", "anon"); | ||
filterChainDefinitionMap.put("/druid/**", "anon"); | ||
filterChainDefinitionMap.put("/logout", "logout"); | ||
filterChainDefinitionMap.put("/", "anon"); | ||
filterChainDefinitionMap.put("/**", "user"); | ||
|
||
shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap); | ||
|
||
return shiroFilterFactoryBean; | ||
} | ||
|
||
@Bean | ||
public SecurityManager securityManager(){ | ||
DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); | ||
securityManager.setRealm(shiroRealm()); | ||
securityManager.setRememberMeManager(rememberMeManager()); | ||
return securityManager; | ||
} | ||
|
||
@Bean(name = "lifecycleBeanPostProcessor") | ||
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() { | ||
return new LifecycleBeanPostProcessor(); | ||
} | ||
|
||
@Bean | ||
public ShiroRealm shiroRealm(){ | ||
ShiroRealm shiroRealm = new ShiroRealm(); | ||
return shiroRealm; | ||
} | ||
|
||
/** | ||
* cookie对象 | ||
* @return | ||
*/ | ||
public SimpleCookie rememberMeCookie() { | ||
// 设置cookie名称,对应login.html页面的<input type="checkbox" name="rememberMe"/> | ||
SimpleCookie cookie = new SimpleCookie("rememberMe"); | ||
// 设置cookie的过期时间,单位为秒,这里为一天 | ||
cookie.setMaxAge(86400); | ||
return cookie; | ||
} | ||
|
||
/** | ||
* cookie管理对象 | ||
* @return | ||
*/ | ||
public CookieRememberMeManager rememberMeManager() { | ||
CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager(); | ||
cookieRememberMeManager.setCookie(rememberMeCookie()); | ||
// rememberMe cookie加密的密钥 | ||
cookieRememberMeManager.setCipherKey(Base64.decode("3AvVhmFLUs0KTA3Kprsdag==")); | ||
return cookieRememberMeManager; | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
12.Spring-Boot-Shiro-RememberMe/src/main/java/com/springboot/controller/LoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.springboot.controller; | ||
|
||
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.springboot.pojo.ResponseBo; | ||
import com.springboot.pojo.User; | ||
import com.springboot.util.MD5Utils; | ||
|
||
@Controller | ||
public class LoginController { | ||
|
||
@GetMapping("/login") | ||
public String login() { | ||
return "login"; | ||
} | ||
|
||
@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(); | ||
try { | ||
subject.login(token); | ||
return ResponseBo.ok(); | ||
} catch (UnknownAccountException e) { | ||
return ResponseBo.error(e.getMessage()); | ||
} catch (IncorrectCredentialsException e) { | ||
return ResponseBo.error(e.getMessage()); | ||
} catch (LockedAccountException e) { | ||
return ResponseBo.error(e.getMessage()); | ||
} catch (AuthenticationException e) { | ||
return ResponseBo.error("认证失败!"); | ||
} | ||
} | ||
|
||
@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 "index"; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
12.Spring-Boot-Shiro-RememberMe/src/main/java/com/springboot/dao/UserMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.springboot.dao; | ||
|
||
import org.apache.ibatis.annotations.Mapper; | ||
|
||
import com.springboot.pojo.User; | ||
|
||
@Mapper | ||
public interface UserMapper { | ||
User findByUserName(String userName); | ||
} |
50 changes: 50 additions & 0 deletions
50
12.Spring-Boot-Shiro-RememberMe/src/main/java/com/springboot/pojo/ResponseBo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.springboot.pojo; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class ResponseBo extends HashMap<String, Object>{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
public ResponseBo() { | ||
put("code", 0); | ||
put("msg", "操作成功"); | ||
} | ||
|
||
public static ResponseBo error() { | ||
return error(1, "操作失败"); | ||
} | ||
|
||
public static ResponseBo error(String msg) { | ||
return error(500, msg); | ||
} | ||
|
||
public static ResponseBo error(int code, String msg) { | ||
ResponseBo ResponseBo = new ResponseBo(); | ||
ResponseBo.put("code", code); | ||
ResponseBo.put("msg", msg); | ||
return ResponseBo; | ||
} | ||
|
||
public static ResponseBo ok(String msg) { | ||
ResponseBo ResponseBo = new ResponseBo(); | ||
ResponseBo.put("msg", msg); | ||
return ResponseBo; | ||
} | ||
|
||
public static ResponseBo ok(Map<String, Object> map) { | ||
ResponseBo ResponseBo = new ResponseBo(); | ||
ResponseBo.putAll(map); | ||
return ResponseBo; | ||
} | ||
|
||
public static ResponseBo ok() { | ||
return new ResponseBo(); | ||
} | ||
|
||
@Override | ||
public ResponseBo put(String key, Object value) { | ||
super.put(key, value); | ||
return this; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
12.Spring-Boot-Shiro-RememberMe/src/main/java/com/springboot/pojo/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.springboot.pojo; | ||
|
||
import java.io.Serializable; | ||
import java.util.Date; | ||
|
||
public class User implements Serializable{ | ||
|
||
private static final long serialVersionUID = -5440372534300871944L; | ||
|
||
private Integer id; | ||
private String userName; | ||
private String password; | ||
private Date createTime; | ||
private String status; | ||
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 Date getCreateTime() { | ||
return createTime; | ||
} | ||
public void setCreateTime(Date createTime) { | ||
this.createTime = createTime; | ||
} | ||
public String getStatus() { | ||
return status; | ||
} | ||
public void setStatus(String status) { | ||
this.status = status; | ||
} | ||
|
||
} |
Oops, something went wrong.