shiro整合thymeleaf

thymeleaf

  • 导入依赖
<dependency>
            <groupId>com.github.theborakompanioni</groupId>
            <artifactId>thymeleaf-extras-shiro</artifactId>
            <version>2.0.0</version>
</dependency>
  • 在MyRealm中进行配置

//shiro整合thymeleaf(整合shiroDialect:用来整合shiro thymeleaf)
@Bean
public ShiroDialect getShiroDialect(){
return new ShiroDialect();
}

@Configuration
public class ShiroConfig {

    //shiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Param("securityManager") DefaultWebSecurityManager securityManager){
        ShiroFilterFactoryBean factoryBean = new ShiroFilterFactoryBean();
        //设置安全管理器
        factoryBean.setSecurityManager(securityManager);
        //添加shiro的内置过滤器
        /*
         *anon:无需认证就可以访问
         * authc:必须认证了才能访问
         * user:必须拥有 记住我  功能才能访问
         * perms:拥有对某个资源的权限才能访问
         * role:拥有某个角色权限才能访问
         * */
        //设置filtermap进行拦截
        Map<String, String> filterMap=new LinkedHashMap<>();
        filterMap.put("/toAdd","anon");
        filterMap.put("/toUpdate","authc");

        //授权(正常情况下未授权会跳转到未授权页面)配置授权链条
        filterMap.put("/toAdd","perms[user:add]");
        filterMap.put("/toUpdate","perms[user:update]");

        //设置未授权的请求(未授权页面)
        factoryBean.setUnauthorizedUrl("/unauthorized");

//        filterMap.put("/**","authc");
        //设置过滤器链
        factoryBean.setFilterChainDefinitionMap(filterMap);
        //如果没有权限(如果没有权限就跳转到登录页面)
        factoryBean.setLoginUrl("/toLogin");
        return factoryBean;
    }


    //DefaultWebSecurityManager
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager getWebSecurityManager(@Param("myRealm") MyRealm myRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        //关联MyRealm
        securityManager.setRealm(myRealm);
        return securityManager;
    }

    //创建Real对象
    @Bean
    public MyRealm myRealm(){
        return new MyRealm();
    }

    //shiro整合thymeleaf(整合shiroDialect:用来整合shiro thymeleaf)
    @Bean
    public ShiroDialect getShiroDialect(){
        return new ShiroDialect();
    }
}
  • 测试
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro"
>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/toLogin}">登录</a>
<p th:text="${msg}"></p>
<div shiro:hasPermission="user:add">
    <a th:href="@{/toAdd}">add</a>
</div>
<div shiro:hasPermission="user:update">
    <a th:href="@{/toUpdate}">update</a>
</div>
<a th:href="@{/logOut}">注销系统</a>
</body>
</html>
  • 代码完善

在Myrealm中添加session

//认证登录
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("执行了=>认证doGetAuthorizationInfo");
        //用户名 密码(从数据库中取)
        /*String name="root";
        String password="123456";
        UsernamePasswordToken tokenUser= (UsernamePasswordToken) token;
        if (!tokenUser.getUsername().equals(name)) {
            return null;//如果是null就会抛出异常(UnknownAccountException)
        }*/
        //连接真实数据库
        UsernamePasswordToken userToken= (UsernamePasswordToken) token;
        User user = userService.getUserByUserName(userToken.getUsername());
        if (user==null) {
            return null;//如果是null就会抛出异常(UnknownAccountException)
        }

        Subject currentSubject = SecurityUtils.getSubject();
        Session session = currentSubject.getSession();
        session.setAttribute("loginUser",user);
        //这儿密码可以加密,MD5加密 MD5盐值加密
        //密码认证,shiro做(这儿存一些信息,那么subject就可以获取这些信息)
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");
    }
}
  • index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:shiro="http://www.thymeleaf.org/thymeleaf-extras-shiro"
>
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>首页</h1>
<div th:if="${session.loginUser==null}">
    <a th:href="@{/toLogin}">登录</a>
</div>
<p th:text="${msg}"></p>
<div shiro:hasPermission="user:add">
    <a th:href="@{/toAdd}">add</a>
</div>
<div shiro:hasPermission="user:update">
    <a th:href="@{/toUpdate}">update</a>
</div>
<div th:if="${session.loginUser!=null}">
    <a th:href="@{/logOut}">注销系统</a>
</div>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值