Shiro是一个功能强大且易于使用的Java安全框架,官网:https://shiro.apache.org/。java
主要功能有身份验证、受权、加密和会话管理。
其它特性有Web支持、缓存、测试支持、容许一个用户用另外一个用户的身份进行访问、记住我。web
Shiro有三个核心组件:Subject,SecurityManager和 Realm。spring
Subject:即当前操做“用户”,“用户”并不单单指人,也能够是第三方进程、后台账户或其余相似事物。
SecurityManager:安全管理器,Shiro框架的核心,经过SecurityManager来管理全部Subject,并经过它来提供安全管理的各类服务。
Realm:域,充当了Shiro与应用安全数据间的“桥梁”或者“链接器”。也就是说,当对用户执行认证(登陆)和受权(访问控制)验证时,Shiro会从应用配置的Realm中查找用户及其权限信息。当配置Shiro时,必须至少指定一个Realm,用于认证和(或)受权。数据库
Spring Boot 中整合Shiro,根据引入的依赖包shiro-spring和shiro-spring-boot-web-starter(当前版本都是1.4.2)不一样有两种不一样方法。apache
方法一:引入依赖包shiro-spring缓存
一、IDEA中建立一个新的SpringBoot项目,pom.xml引用的依赖包以下:安全
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring</artifactId> <version>1.4.2</version> </dependency>
二、建立Realm和配置shiroapp
(1)建立Realm框架
package com.example.demo.config; import org.apache.shiro.authc.*; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; public class MyRealm extends AuthorizingRealm { /**权限信息,暂不实现*/ @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { return null; } /**身份认证:验证用户输入的帐号和密码是否正确。*/ @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { //获取用户输入的帐号 String userName = (String) token.getPrincipal(); //验证用户admin和密码123456是否正确 if (!"admin".equals(userName)) { throw new UnknownAccountException("帐户不存在!"); } SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(userName, "123456", getName()); return authenticationInfo; //实际项目中,上面帐号从数据库中获取用户对象,再判断是否存在 /*User user = userService.findByUserName(userName); if (user == null) { throw new UnknownAccountException("帐户不存在!"); } SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user,user.getPassword(), getName()); return authenticationInfo; */ } }
(2)配置Shirojsp
package com.example.demo.config; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.LinkedHashMap; import java.util.Map; @Configuration public class ShiroConfig { @Bean MyRealm myRealm() { return new MyRealm(); } @Bean DefaultWebSecurityManager securityManager() { DefaultWebSecurityManager manager = new DefaultWebSecurityManager(); manager.setRealm(myRealm()); return manager; } @Bean ShiroFilterFactoryBean shiroFilterFactoryBean() { ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean(); bean.setSecurityManager(securityManager()); //若是不设置默认会自动寻找Web工程根目录下的"/login.jsp"页面 bean.setLoginUrl("/login"); //登陆成功后要跳转的连接 bean.setSuccessUrl("/index"); //未受权界面 bean.setUnauthorizedUrl("/403"); //配置不会被拦截的连接 Map<String, String> map = new LinkedHashMap<>(); map.put("/doLogin", "anon"); map.put("/**", "authc"); bean.setFilterChainDefinitionMap(map); return bean; } }
三、控制器测试方法
package com.example.demo.controller; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.UnknownAccountException; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.subject.Subject; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class LoginController { @GetMapping("/login") public String login() { return "登陆页面..."; } @PostMapping("/doLogin") public String doLogin(String userName, String password) { Subject subject = SecurityUtils.getSubject(); try { subject.login(new UsernamePasswordToken(userName, password)); return "登陆成功!"; } catch (UnknownAccountException e) { return e.getMessage(); } catch (AuthenticationException e) { return "登录失败,密码错误!"; } } //若是没有先登录,访问会跳到/login @GetMapping("/index") public String index() { return "index"; } @GetMapping("/403") public String unauthorizedRole(){ return "没有权限"; } }
方法二:引入依赖包shiro-spring-boot-web-starter
一、pom.xml中删除shiro-spring,引入shiro-spring-boot-web-starter
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-spring-boot-web-starter</artifactId> <version>1.4.2</version> </dependency>
二、建立Realm和配置shiro
(1)建立Realm,代码和方法一的同样。
(2)配置Shiro
package com.example.demo.config; import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition; import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class ShiroConfig { @Bean MyRealm myRealm() { return new MyRealm(); } @Bean DefaultWebSecurityManager securityManager() { DefaultWebSecurityManager manager = new DefaultWebSecurityManager(); manager.setRealm(myRealm()); return manager; } @Bean ShiroFilterChainDefinition shiroFilterChainDefinition() { DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition(); definition.addPathDefinition("/doLogin", "anon"); definition.addPathDefinition("/**", "authc"); return definition; } }
(3)application.yml配置
shiro: unauthorizedUrl: /403 successUrl: /index loginUrl: /login