资讯详情

一: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.编写登录页面

4.用于控制层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 = SecurityUils.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>

标签: xtkj智能传感器

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司