shiro整合mybatis,thymeleaf

配置文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.flash</groupId>
    <artifactId>shiro-spring</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>shiro-spring</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-spring -->
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring</artifactId>
            <version>1.6.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.github.theborakompanioni/thymeleaf-extras-shiro -->
        <dependency>
            <groupId>com.github.theborakompanioni</groupId>
            <artifactId>thymeleaf-extras-shiro</artifactId>
            <version>2.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.3</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

shiroConfig

package com.flash.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig {
    //3
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("getdefaultWebSecurityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);


//        anon:无需认证即可访问
//        authc:必须认证了才能访问
//        user:必须用用记住我功能才能使用
//        perms:拥有对某个资源的权限才能访问
//        role:拥有某个角色权才能访问

        LinkedHashMap<String,String> filtermap = new LinkedHashMap<>();

        filtermap.put("/user/add","perms[user:add]");
        filtermap.put("/user/update","perms[user:update]");
     //   filtermap.put("/user/*","authc");
        bean.setFilterChainDefinitionMap(filtermap);
        bean.setLoginUrl("/tologin");
        bean.setUnauthorizedUrl("/unauth");
        return bean;
    }

    //2
    @Bean
    public DefaultWebSecurityManager getdefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(userRealm);
        return defaultWebSecurityManager;
    }


    //创建Realm对象 1
    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }


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

AuthorizingRealm

package com.flash.config;

import com.flash.pojo.User;
import com.flash.service.UserServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.LinkedHashMap;

public class UserRealm extends AuthorizingRealm {

    @Autowired
    UserServiceImpl userService;

    @Override//授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行了授权doGetAuthorizationInfo");

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        //拿到当前这个对象
        Subject subject = SecurityUtils.getSubject();
        User currentUser = (User) subject.getPrincipal();

        info.addStringPermission(currentUser.getPermission());


        return info;
    }

    @Override//认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("执行了认证doGetAuthenticationInfo");




        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        User user = userService.quarryUserListByAccount(userToken.getUsername());




        if(user==null)
            return null;//自动抛出UnknownAccountException异常

        Subject CurrSubject = SecurityUtils.getSubject();
        Session session = CurrSubject.getSession();
        session.setAttribute("loginUser",user);


//        //加密盐值
//        //盐值通常取唯一的,我们这用用户名作为盐值
//        ByteSource salt = ByteSource.Util.bytes(userToken.getUsername());//盐值加密
//        //加密算法
//        String hashAlgorithName="MD5";
//        //加密明文
//        String credentials=user.getPassword();
//        //加密次数
//        int hashIterations = 1024;
//        Object password = new SimpleHash(hashAlgorithName,credentials,salt,hashIterations);
//        System.out.println(password);
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");//密码管理shiro来做
    }
}

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.pollix.at/thymeleaf/shiro" >


<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<p1>首页</p1>
<p th:text="${msg}"></p>

<!--从session中判断值-->
<div th:if="${session.loginUser==null}">
    <a th:href="@{/tologin}">登陆</a>
</div>
<div shiro:hasPermission="user:add">
<a th:href="@{user/add}">add</a>
</div>

<div shiro:hasPermission="user:update">
<a th:href="@{user/update}">update</a>
</div>
</body>
</html>

tologin.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>登陆</h1>
<p th:text="${msg}" style="color: red"></p>
<form th:action="@{/login}">
    <p>用户名  <input type="text" name="username"></p>
    <p>密码  <input type="password" name="password"></p>
    <p><input type="submit"> </p>

</form>
</body>
</html>

application.yml

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/PersonManage?useUnicode=true&characterEncoding=utf-8&useSSL=false
    driver-class-name: com.mysql.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    # 下面为连接池的补充设置,应用到上面所有数据源中
    # 初始化大小,最小,最大
    initial-size: 5
    min-idle: 5
    max-active: 20
    # 配置获取连接等待超时的时间
    max-wait: 60000
    # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
    time-between-eviction-runs-millis: 60000
    # 配置一个连接在池中最小生存的时间,单位是毫秒
    min-evictable-idle-time-millis: 300000
    validation-query: SELECT 1 FROM DUAL
    test-while-idle: true
    test-on-borrow: false
    test-on-return: false
    # 打开PSCache,并且指定每个连接上PSCache的大小
    pool-prepared-statements: true


    #   配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
    filters: stat,wall,log4j
    max-pool-prepared-statement-per-connection-size: 20
    use-global-data-source-stat: true
    # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
    connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500


mybatis:
   type-aliases-package: com.flash.pojo
   mapper-locations: classpath:mapper/*.xml


mybatis相关配置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个基于Spring框架的快速开发框架,通过提供一系列的开箱即用的功能和优化配置,简化了Java后台应用的开发流程。 Thymeleaf是一个Java模板引擎,用于在服务端渲染HTML页面。它可以和Spring Boot结合使用,通过在HTML页面中使用Thymeleaf的语法,实现页面的动态渲染和数据绑定。 Layui是一个国内比较流行的前端UI框架,提供了大量的CSS样式和JavaScript组件,可以快速构建美观、响应式的前端界面。 Apache Shiro是一个强大的开源安全框架,可以用于认证、授权和加密操作。它提供了对用户身份验证、角色和权限管理的支持,可以帮助开发者快速实现应用的安全控制。 Redis是一个高性能的内存数据库,常用于缓存和存储数据。它支持多种数据结构和操作,可以用于实现分布式锁、消息队列等功能,提高系统的性能和可扩展性。 MyBatis Plus是一个基于MyBatis框架的增强工具,提供了更简单、更便捷的数据库操作方式。它通过代码生成器和一系列的增强功能,简化了数据层的开发工作,提高了开发效率。 综上所述,可以使用Spring Boot作为后台框架,集成Thymeleaf实现页面渲染和数据绑定,使用Layui构建前端界面,使用Apache Shiro进行安全控制,使用Redis进行数据缓存和存储,使用MyBatis Plus进行数据库操作。这样搭建的后台系统可以实现高效、安全、可扩展的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值