# 配置
导入包
| <dependencies> |
| <dependency> |
| <groupId>org.apache.shiro</groupId> |
| <artifactId>shiro-all</artifactId> |
| <version>1.10.0</version> |
| </dependency> |
| </dependencies> |
新建配置文件 shiro.ini
| |
| [users] |
| admin=123456 |
| user01=123456 |
# 使用
新建测试类 ShiroTest.java
| import org.apache.shiro.SecurityUtils; |
| import org.apache.shiro.authc.*; |
| import org.apache.shiro.authz.AuthorizationException; |
| import org.apache.shiro.config.IniSecurityManagerFactory; |
| import org.apache.shiro.mgt.SecurityManager; |
| import org.apache.shiro.subject.Subject; |
| |
| import java.util.Scanner; |
| |
| public class ShiroTest { |
| public static void main(String[] args) { |
| |
| Scanner input = new Scanner(System.in); |
| System.out.print("请输入用户名:"); |
| String userName = input.next(); |
| System.out.print("请输入密码:"); |
| String password = input.next(); |
| |
| |
| IniSecurityManagerFactory securityManagerFactory = |
| new IniSecurityManagerFactory("classpath:shiro.ini"); |
| |
| SecurityManager securityManager = securityManagerFactory.getInstance(); |
| |
| SecurityUtils.setSecurityManager(securityManager); |
| |
| |
| Subject subject = SecurityUtils.getSubject(); |
| |
| AuthenticationToken token = new UsernamePasswordToken(userName,password); |
| |
| try { |
| subject.login(token); |
| System.out.println("登录成功!"); |
| } catch (AuthenticationException e) { |
| System.out.println("登录失败!"); |
| } |
| } |
| } |
# 登录的一些异常
| public class ShiroTest { |
| public static void main(String[] args) { |
| |
| |
| |
| Subject subject = SecurityUtils.getSubject(); |
| |
| AuthenticationToken token = new UsernamePasswordToken(userName,password); |
| |
| try { |
| subject.login(token); |
| |
| subject.checkRole("user"); |
| System.out.println("登录成功!"); |
| } catch (AccountException e) { |
| System.out.println("用户名不存在!"); |
| } catch (CredentialsException e) { |
| System.out.println("密码错误!"); |
| } catch (AuthenticationException e) { |
| System.out.println("登录失败!"); |
| } |
| } |
| } |
# 添加角色
修改 shiro.ini
| |
| [users] |
| admin=123456,user,admin |
| user01=123456,user |
| |
| |
修改测试类 ShiroTest.java
| public class ShiroTest { |
| public static void main(String[] args) { |
| |
| |
| |
| |
| Subject subject = SecurityUtils.getSubject(); |
| |
| AuthenticationToken token = new UsernamePasswordToken(userName,password); |
| |
| try { |
| subject.login(token); |
| |
| |
| if (subject.hasRole("user")) { |
| System.out.println("包含 user 角色 !"); |
| } else { |
| System.out.println("不包含 user 角色!"); |
| } |
| |
| System.out.println("登录成功!"); |
| } catch (AuthenticationException e) { |
| System.out.println("登录失败!"); |
| } |
| } |
| } |
以上代码是用 if 校验的角色信息,除了这种方式还能使用 Subject 中的 checkRole 方法 抛出异常的方式校验角色信息
| public class ShiroTest { |
| public static void main(String[] args) { |
| |
| |
| |
| |
| Subject subject = SecurityUtils.getSubject(); |
| |
| AuthenticationToken token = new UsernamePasswordToken(userName,password); |
| |
| try { |
| subject.login(token); |
| |
| subject.checkRole("user"); |
| System.out.println("登录成功!"); |
| } catch (AuthenticationException e) { |
| System.out.println("登录失败!"); |
| } catch (AuthorizationException e) { |
| System.out.println("角色不匹配!"); |
| } |
| } |
| } |
# 添加角色权限