shiro教程1(HelloWorld)

简介: shiro教程1(HelloWorld)

image.png


image.pngimage.png

内部结构框架

image.pngimage.pngimage.pngimage.pngimage.png

#创建对象
securityManager=org.apache.shiro.mgt.DefaultSecurityManager 

image.png

[users] 
zhang=123,role1,role2 
wang=123 

image.png

[roles] 
role1=user:create,user:update 
role2=*  

image.png

/index.html = anon 
/admin/** = authc, roles[admin],perms["permission1"]

image.png

<dependency>
  <groupId>org.apache.shiro</groupId>
  <artifactId>shiro-core</artifactId>
  <version>1.1.0</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-simple</artifactId>
  <version>1.6.1</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>commons-logging</groupId>
  <artifactId>commons-logging</artifactId>
  <version>1.2</version>
</dependency>

image.png

[users]
root = 123456
# 账号为root 密码是 123456

认证操作

@Test
public void test() {
  // 1.获取SecurityManager工厂对象
  Factory<SecurityManager> factory = 
      new IniSecurityManagerFactory("classpath:shiro.ini");
  // 2.通过Factory对象获取SecurityManager对象
  SecurityManager securityManager = factory.getInstance();
  // 3.将SecurityManager对象添加到当前运行环境中
  SecurityUtils.setSecurityManager(securityManager);
  // 4.获取Subject对象
  Subject subject = SecurityUtils.getSubject();
  AuthenticationToken token = new UsernamePasswordToken("root", "123456");
  // 登录操作
  subject.login(token);
  // 获取登录的状态
  System.out.println(subject.isAuthenticated());
}

image.pngimage.pngimage.png

  @Test
  public void test() {
    // 1.获取SecurityManager工厂对象
    Factory<SecurityManager> factory = 
        new IniSecurityManagerFactory("classpath:shiro.ini");
    // 2.通过Factory对象获取SecurityManager对象
    SecurityManager securityManager = factory.getInstance();
    // 3.将SecurityManager对象添加到当前运行环境中
    SecurityUtils.setSecurityManager(securityManager);
    // 4.获取Subject对象
    Subject subject = SecurityUtils.getSubject();
    AuthenticationToken token = new UsernamePasswordToken("root", "111");
    // 登录操作
    try {
      subject.login(token);
    } catch (UnknownAccountException e) {
      System.out.println("账号出错...");
    } catch(IncorrectCredentialsException e){
      System.out.println("密码出错...");
    }
    // 获取登录的状态
    System.out.println(subject.isAuthenticated());
  }

认证流程总结

image.pngimage.pngimage.pngimage.pngimage.pngimage.png

相关文章
|
3月前
|
Java Maven
SpringBoot快速入门,写一个简单的HelloWorld文件
SpringBoot快速入门,写一个简单的HelloWorld文件
|
4月前
|
前端开发 Java Maven
SpringMVC 写个 HelloWorld
SpringMVC 写个 HelloWorld
24 0
|
4月前
|
Java 应用服务中间件 Maven
SpringBoot - HelloWorld与简解
SpringBoot - HelloWorld与简解
44 0
|
11月前
|
缓存 算法 安全
快速学会如何使用Shiro
快速学会如何使用Shiro
101 2
|
Java API 数据库
Shiro学习之Shiro基本使用(1)
Shiro学习之Shiro基本使用(1)
77 0
|
安全 前端开发 Java
JavaWeb|Springboot整合Shiro实现登录验证
JavaWeb|Springboot整合Shiro实现登录验证
106 0
|
缓存 安全 算法
SpringSecurity笔记之helloworld
SpringSecurity笔记之helloworld
74 0
SpringSecurity笔记之helloworld
|
缓存 安全 Java
SpringBoot整合Shiro_HelloWorld
SpringBoot整合Shiro_HelloWorld
123 0
SpringBoot整合Shiro_HelloWorld
|
Java 程序员 项目管理
SpringBoot入门系列HelloWorld
SpringBoot入门系列HelloWorld
126 0
SpringBoot入门系列HelloWorld
|
前端开发 Java Spring
springMVC 入门程序 helloworld!
springMVC 入门程序 helloworld!
117 0