zoukankan      html  css  js  c++  java
  • springboot整合shiro步骤

    一:springboot快速入门:

    1.建立Maven项目,导入springboot父工程

    <!-- 继承springboot默认父工程 -->
    <parent>
      	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-parent</artifactId>
      	<version>2.1.2.RELEASE</version>
    </parent> 
    

    2.导入web支持

    <!-- 导入web场景支持 -->
    <dependency>
      	<groupId>org.springframework.boot</groupId>
      	<artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    

    3.编写测试controller类

    4.编写springboot启动类

    @SpringBootApplication
    public class Application {
    	public static void main(String[] args) {
    		SpringApplication.run(Application.class, args);
    	}
    }
    

    5.导入thymeleaf页面模块

    1.引入thymeleaf依赖

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    

    2.建立test.html页面

    在src/main/resource/创建templates目录,再创建test.html页面

    再thymeleaf3.0以前对页面标签语法要求比较严格,开始标签必须有对应的结束标签

    二:springboot与shiro整合

    1.shiro核心api

    subject:用户主体(把操作交给securityManager)
    securityManager:安全管理器(关联realm)
    reaml:shiro连接数据的桥梁	
    

    2.springboot整合shiro

    2.1导如shiro与spring整合依赖

    <dependency>
      <groupId>org.apache.shiro</groupId>
      <artifactId>shiro-spring</artifactId>
      <version>1.4.0</version>
    </dependency>
    

    2.2自定义realm类,继承AuthorizingRealm

    2.3编写shiro配置类

    创建ShiroFilterFactoryBean

    创建DefaultWebSecurityManager

    创建Realm

    3.使用shiro内置过滤器实现页面拦截

    在ShiroFilterFactoryBean里添加shiro内置过滤器

    shiro内置过滤器,可以实现权限相关的拦截器

    //常用的过滤器:
    	//anon:无需认证(登陆)可以访问
    	//authc:必须认证才可以访问
    	//user:如果使用rememberMe的功能可以直接访问
    	//perms:该资源必须得到资源权限才可以访问
    	//role:该资源必须得到角色权限才可以访问
    Map<String,String> filterMap = new LinkedHashMap<String,String>();
    	filterMap.put("/testThymeleaf", "anon");	//放行
    	filterMap.put("/*", "authc");	//拦截
    	//修改跳转到的登陆页面,默认是跳到login.jsp
    	shiroFilterFactoryBean.setLoginUrl("/tologin");
    	shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);
    

    4.实现用户认证(登陆)操作

    4.1编写登陆页面

    4.2控制层中使用shiro编写认证操作

    //获取subject
        Subject subject = SecurityUtils.getSubject();
    //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
    //执行登陆方法
        subject.login(token);
    

    4.3编写realm的判断逻辑

    //1.判断用户名
    UsernamePasswordToken token = (UsernamePasswordToken)arg0;
    if(!token.getUsername().equals(username)) {
    	//用户名不存在
    	return null;	//shiro底层会抛出UnknownAccountException
    }
    //2.判断密码
    return new SimpleAuthenticationInfo("", password, "");
    

    5.整合mybatis实现认证登陆

    5.1导入mybatis相关依赖包

    <!-- 数据库连接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.9</version>
    </dependency>
    <!-- mysql数据库 -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.44</version>
    </dependency>
    <!-- springboot的mybatis启动器 -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.1.1</version>
    </dependency>
    

    5.2配置application.properties

    位置在:src/main/resources目录下:配置jdbc,连接池,扫描包

    5.3编写User实体

    5.4编写UserMapper接口

    5.5编写UserMapper.xml映射文件

    5.6编写业务接口和实现

    接口/实现

    5.7添加@MapperScan注解

    //在spingboot启动程序类上加上:
    @MapperScan("com.xtkj.mapper")
    

    5.8修改UserRealm

    User user = userService.findByAccount(token.getUsername());
    if(null==user) {
    	//用户名不存在
    	return null;	//shiro底层会抛出UnknownAccountException
    }
    

    三:springboot与shiro整合实现用户授权

    1.使用shiro内置过滤器拦截资源(ShiroConfig)

    	//授权过滤器(需要放在下面认证语句之前)
    	//当授权拦截后,shiro会自动跳转到未授权页面
    	filterMap.put("/add", "perms[user:add]");
    	filterMap.put("/update", "perms[user:update]");
    	//设置未授权页面
    	shiroFilterFactoryBean.setUnauthorizedUrl("/unauth");
    

    2.完成shiro的资源授权(UserRealm)

    	//给资源进行授权
    	SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
    	//到数据库查询当前登陆用户的授权字符串
    	//获取当前登陆用户
    	Subject subject = SecurityUtils.getSubject();
    	User user = (User)subject.getPrincipal();
    	User dbuser = userService.findById(user.getId());
    	//根据角色来进行授权
    	info.addStringPermission(dbuser.getPerms());
    

    四:thymeleaf和shiro标签整合使用

    1.导入thymeleaf扩展坐标

    <!-- thymeleaf对shiro的扩展坐标 -->
    <dependency> 
      <groupId>com.github.theborakompanioni</groupId> 
      <artifactId>thymeleaf-extras-shiro</artifactId> 
      <version>2.0.0</version> 
    </dependency>
    

    2.配置shiroDialect

    @Bean
    public ShiroDialect getShiroDialect() {
      return new ShiroDialect();
    }
    

    3.在html页面上使用shiro标签

    <div shiro:hasPermission="user:add">
    	进入用户添加页面:<a th:href="@{/add}">用户添加</a><br>
    </div>
    <div shiro:hasPermission="user:update">
    	进入用户修改页面:<a th:href="@{/update}">用户修改</a><br>
    </div>
  • 相关阅读:
    [CF1284E]New Year and Castle Construction
    [BZOJ2178]圆的面积并(格林公式)
    [CF1146H]Satanic Panic(dp)
    [CF1019D]Large Triangle
    [ICPC World Finals 2018][LOJ6409]熊猫保护区(voronoi图)
    [CF gym 101471A][LOJ6470]Airport Construction
    [BZOJ2809]dispatching(左偏树)
    [HDU5784]How Many Triangles
    [CF372E]Drawing circles is fun(反演)
    [NOI2005][BZOJ1502][洛谷P4207]月下柠檬树(自适应Simpson积分)
  • 原文地址:https://www.cnblogs.com/itzlg/p/10699527.html
Copyright © 2011-2022 走看看