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
e50f57b
commit 5c6e3c3
Showing
34 changed files
with
1,481 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,103 @@ | ||
<?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>demo</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> | ||
|
||
<!-- shiro ehcache --> | ||
<dependency> | ||
<groupId>org.apache.shiro</groupId> | ||
<artifactId>shiro-ehcache</artifactId> | ||
<version>1.3.2</version> | ||
</dependency> | ||
<!-- ehchache --> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-cache</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>net.sf.ehcache</groupId> | ||
<artifactId>ehcache</artifactId> | ||
</dependency> | ||
|
||
<!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro --> | ||
<dependency> | ||
<groupId>com.github.theborakompanioni</groupId> | ||
<artifactId>thymeleaf-extras-shiro</artifactId> | ||
<version>2.0.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
17.Spring-Boot-Shiro-Session/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); | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
17.Spring-Boot-Shiro-Session/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,133 @@ | ||
package com.springboot.config; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.LinkedHashMap; | ||
|
||
import org.apache.shiro.cache.ehcache.EhCacheManager; | ||
import org.apache.shiro.codec.Base64; | ||
import org.apache.shiro.mgt.SecurityManager; | ||
import org.apache.shiro.session.SessionListener; | ||
import org.apache.shiro.session.mgt.SessionManager; | ||
import org.apache.shiro.session.mgt.eis.MemorySessionDAO; | ||
import org.apache.shiro.session.mgt.eis.SessionDAO; | ||
import org.apache.shiro.spring.LifecycleBeanPostProcessor; | ||
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor; | ||
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.apache.shiro.web.session.mgt.DefaultWebSessionManager; | ||
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.DependsOn; | ||
|
||
import com.springboot.listener.ShiroSessionListener; | ||
import com.springboot.shiro.ShiroRealm; | ||
|
||
import at.pollux.thymeleaf.shiro.dialect.ShiroDialect; | ||
|
||
@Configuration | ||
public class ShiroConfig { | ||
|
||
@Bean | ||
public EhCacheManager getEhCacheManager() { | ||
EhCacheManager em = new EhCacheManager(); | ||
em.setCacheManagerConfigFile("classpath:config/shiro-ehcache.xml"); | ||
return em; | ||
} | ||
|
||
@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()); | ||
securityManager.setCacheManager(getEhCacheManager()); | ||
securityManager.setSessionManager(sessionManager()); | ||
return securityManager; | ||
} | ||
|
||
@Bean(name = "lifecycleBeanPostProcessor") | ||
public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() { | ||
return new LifecycleBeanPostProcessor(); | ||
} | ||
|
||
@Bean | ||
public ShiroRealm shiroRealm(){ | ||
ShiroRealm shiroRealm = new ShiroRealm(); | ||
return shiroRealm; | ||
} | ||
|
||
public SimpleCookie rememberMeCookie() { | ||
SimpleCookie cookie = new SimpleCookie("rememberMe"); | ||
cookie.setMaxAge(86400); | ||
return cookie; | ||
} | ||
|
||
public CookieRememberMeManager rememberMeManager() { | ||
CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager(); | ||
cookieRememberMeManager.setCookie(rememberMeCookie()); | ||
cookieRememberMeManager.setCipherKey(Base64.decode("4AvVhmFLUs0KTA3Kprsdag==")); | ||
return cookieRememberMeManager; | ||
} | ||
|
||
@Bean | ||
@DependsOn({"lifecycleBeanPostProcessor"}) | ||
public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() { | ||
DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator(); | ||
advisorAutoProxyCreator.setProxyTargetClass(true); | ||
return advisorAutoProxyCreator; | ||
} | ||
|
||
@Bean | ||
public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) { | ||
AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor(); | ||
authorizationAttributeSourceAdvisor.setSecurityManager(securityManager); | ||
return authorizationAttributeSourceAdvisor; | ||
} | ||
|
||
@Bean | ||
public ShiroDialect shiroDialect() { | ||
return new ShiroDialect(); | ||
} | ||
|
||
@Bean | ||
public SessionDAO sessionDAO() { | ||
MemorySessionDAO sessionDAO = new MemorySessionDAO(); | ||
return sessionDAO; | ||
} | ||
|
||
@Bean | ||
public SessionManager sessionManager() { | ||
DefaultWebSessionManager sessionManager = new DefaultWebSessionManager(); | ||
Collection<SessionListener> listeners = new ArrayList<SessionListener>(); | ||
listeners.add(new ShiroSessionListener()); | ||
sessionManager.setSessionListeners(listeners); | ||
sessionManager.setSessionDAO(sessionDAO()); | ||
return sessionManager; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
17.Spring-Boot-Shiro-Session/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,65 @@ | ||
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"; | ||
} | ||
|
||
@GetMapping("/403") | ||
public String forbid() { | ||
return "403"; | ||
} | ||
|
||
@RequestMapping("/index") | ||
public String index(Model model) { | ||
User user = (User) SecurityUtils.getSubject().getPrincipal(); | ||
model.addAttribute("user", user); | ||
return "index"; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
17.Spring-Boot-Shiro-Session/src/main/java/com/springboot/controller/SessionController.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,45 @@ | ||
package com.springboot.controller; | ||
|
||
import java.util.List; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import com.springboot.pojo.ResponseBo; | ||
import com.springboot.pojo.UserOnline; | ||
import com.springboot.service.SessionService; | ||
|
||
|
||
@Controller | ||
@RequestMapping("/online") | ||
public class SessionController { | ||
|
||
@Autowired | ||
SessionService sessionService; | ||
|
||
@RequestMapping("index") | ||
public String online() { | ||
return "online"; | ||
} | ||
|
||
@ResponseBody | ||
@RequestMapping("list") | ||
public List<UserOnline> list() { | ||
return sessionService.list(); | ||
} | ||
|
||
@ResponseBody | ||
@RequestMapping("forceLogout") | ||
public ResponseBo forceLogout(String id) { | ||
try { | ||
sessionService.forceLogout(id); | ||
return ResponseBo.ok(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return ResponseBo.error("踢出用户失败"); | ||
} | ||
|
||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
17.Spring-Boot-Shiro-Session/src/main/java/com/springboot/controller/UserController.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,33 @@ | ||
package com.springboot.controller; | ||
|
||
import org.apache.shiro.authz.annotation.RequiresPermissions; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
@Controller | ||
@RequestMapping("/user") | ||
public class UserController { | ||
|
||
|
||
@RequiresPermissions("user:user") | ||
@RequestMapping("list") | ||
public String userList(Model model) { | ||
model.addAttribute("value", "获取用户信息"); | ||
return "user"; | ||
} | ||
|
||
@RequiresPermissions("user:add") | ||
@RequestMapping("add") | ||
public String userAdd(Model model) { | ||
model.addAttribute("value", "新增用户"); | ||
return "user"; | ||
} | ||
|
||
@RequiresPermissions("user:delete") | ||
@RequestMapping("delete") | ||
public String userDelete(Model model) { | ||
model.addAttribute("value", "删除用户"); | ||
return "user"; | ||
} | ||
} |
Oops, something went wrong.