springboot2.x整合shiro +themleaf

1.项目结构:

2.先整合themleaf

结构:

步骤:

1.在pom.xml中加入依赖:

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

如果创建项目的时候没有选择web,也需要把web依赖加上:

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

2.在resources -> templates包下新建一个index.html页面,这个是 命名空间声明,不加报错很烦:xmlns:th="http://www.thymeleaf.org"

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<th th:text="${msg}"></th>
<!--<br>
<a th:href="@{/user/add}">添加</a>
<br>
<a th:href="@{/user/update}">更新</a>-->
</body>
</html>

3.编写一个controller,如下:

package com.studys.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    @RequestMapping("/index")
    public String toIndex(Model model){
        model.addAttribute("msg","hello,shiro");
    return "index";
    }

  /*  @RequestMapping("/user/add")
    public String userAdd(Model model){
        model.addAttribute("msg","hello,shiro");
        return "user/userAdd";
    }

    @RequestMapping("/user/update")
    public String userUpdate(Model model){
        model.addAttribute("msg","hello,shiro");
        return "user/userUpdate";
    }*/

}

然后启动项目:访问http://localhost:8080/index    看见以下界面:ok

3.接下来整合shiro

1.加入pom.xml依赖

        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring-boot-web-starter</artifactId>
            <version>1.5.1</version>
        </dependency>

2.增加如下文件,后面直接贴文件代码:

1.UserRealm

/**
 * @Description  自定义Realm,shiroConfig中需要用到
 * @Author tc
 * @Date 11:55 2020-06-19
 * @Param 
 * @return 
 **/
public class UserRealm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.printf("开始授权。。。。。。。。。。。");

        return null;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.printf("开始认证。。。。。。。。。。。");
        return null;
    }
}

2.ShiroConfig,这里是一个配置类,让spring ioc
 容器来管理这些bean

@Configuration
public class ShiroConfig {

   /* //配置过滤器
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean bean= new ShiroFilterFactoryBean();
        bean.setSecurityManager(defaultWebSecurityManager);
        return bean;
    }

    //配置管理器
    @Bean(name="securityManager")
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("getUserRealm") UserRealm userRealm){
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();
        securityManager.setRealm(userRealm);
        return securityManager;
    }*/


    //配置过滤器  (编写顺序3)
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition definition = new DefaultShiroFilterChainDefinition();
        //这里配置过滤的规则(根据自己需要配置)
       /* definition.addPathDefinition("/login", "anon");
        definition.addPathDefinition("/**", "authc");*/
        return definition;
    }

    //配置管理器    (编写顺序2-需要依赖1)
    @Bean
    public DefaultWebSecurityManager securityManager(@Qualifier("getUserRealm") UserRealm userRealm) {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
        //这里将userRealm设置斤管理器里,可以用@Qualifier("getUserRealm"),也可以像下面这么用
        // manager.setRealm(getUserRealm())
        manager.setRealm(userRealm);
        return manager;
    }

    //配置自定义Realm(自定义登录验证的逻辑)  (编写顺序1)
    @Bean
    public UserRealm getUserRealm(){
        UserRealm userRealm=new UserRealm();
        return userRealm;
    }
}

3.UserController


@Controller
public class UserController {

    @RequestMapping("/index")
    public String toIndex(Model model){
        model.addAttribute("msg","hello,shiro");
    return "index";
    }

    @RequestMapping("/user/add")
    public String userAdd(Model model){
        model.addAttribute("msg","hello,shiro");
        return "user/userAdd";
    }

    @RequestMapping("/user/update")
    public String userUpdate(Model model){
        model.addAttribute("msg","hello,shiro");
        return "user/userUpdate";
    }

}

4.userAdd.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<h1>这是用户添加页面</h1>
</body>
</html>

5.userUpdate.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>这是用户更新页面</h1>
</body>
</html>

5.index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<th th:text="${msg}"></th>
<br>
<a th:href="@{/user/add}">添加</a>
<br>
<a th:href="@{/user/update}">更新</a>
</body>
</html>

最后启动测试:如下

点击添加:

点击更新:

后面的认证逻辑,和授权逻辑,下回分解。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值