• SpringBoot 整合Shiro


    参考 【狂神说Java】SpringBoot最新教程IDEA版通俗易懂

    1、pom

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

    2、ShiroConfig

    @Configuration
    public class ShiroConfig {
    
        //ShiroFilterFactoryBean
        @Bean
        public ShiroFilterFactoryBean getShiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager) {
            ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
            //设置安全管理器
            bean.setSecurityManager(securityManager);
            //设置内部拦截器
            Map<String, String> filterMap = new HashMap<>();
            filterMap.put("/user/*", "authc");
            bean.setFilterChainDefinitionMap(filterMap);
            bean.setLoginUrl("/toLogin");
            return bean;
        }
    
        //DefaultSecurityManager
        @Bean(name = "securityManager")
        public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("userRealm") UserRealm userRealm) {
            DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
            //关联
            securityManager.setRealm(userRealm);
            return securityManager;
        }
    
        //创建Realm对象
        @Bean(name = "userRealm")
        public UserRealm getUserRealm() {
            return new UserRealm();
        }
    }
    

    3、UserRealm

    //自定义的UserReal
    public class UserRealm extends AuthorizingRealm {
        //授权
        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
            System.out.println("执行了->授权doGetAuthorizationInfo");
            return null;
        }
    
        //认证
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
            System.out.println("执行了->认证doGetAuthenticationInfo");
            String username = "root";
            String password = "123456";
            UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
            if (!username.equals(token.getUsername())) {
                return null;
            }
            return new SimpleAuthenticationInfo("", password, "");
        }
    }
    

    4、IndexController

    @Controller
    public class IndexController {
        @RequestMapping({"/", "index"})
        public String toIndex(Model model) {
            model.addAttribute("msg", "hello,shiro");
            return "index";
        }
    
        @RequestMapping("/user/add")
        public String toAdd() {
            return "user/add";
        }
    
        @RequestMapping("/user/update")
        public String toUpdate() {
            return "user/update";
        }
    
        @RequestMapping("/toLogin")
        public String toLogin() {
            return "login";
        }
    
        @RequestMapping("/login")
        public String login(String username, String password, Model model) {
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken token = new UsernamePasswordToken(username, password);
            try {
                subject.login(token);
                return "index";
            } catch (UnknownAccountException e) {
                model.addAttribute("msg", "用户名错误");
                return "login";
            } catch (IncorrectCredentialsException e) {
                model.addAttribute("msg", "密码错误");
                return "login";
            }
        }
    }
    

    5、html

    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>add</h1>
    </body>
    </html>
    
    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
    <h1>update</h1>
    </body>
    </html>
    
    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org"
          xmlns:sec="http://www.thymeleaf.org/extras/spring-security"
          xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
    <head>
        <meta charset="UTF-8">
        <title>Index</title>
    </head>
    <body>
    <p th:text="${msg}"></p>
    
    <a th:href="@{user/add}">user/add</a>
    <a th:href="@{user/update}">user/update</a>
    </body>
    </html>
    
    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>login</title>
    </head>
    <body>
    <h1>登录</h1>
    <form th:action="@{/login}" method="post">
        <p><input type="text" name="username"></p>
        <p><input type="text" name="password"></p>
        <p><input type="submit" value="提交"></p>
    </form>
    </body>
    </html>
    
  • 相关阅读:
    基于小程序开发的藏书馆
    picker(级联)组件及组件封装经验
    秒杀组件开发-可实现多种倒计时功能
    async/await 与 generator、co 的对比
    nodejs项目总结
    小程序开发小结-线下服务器域名部署等
    性能提速:debounce(防抖)、throttle(节流/限频)
    vuex数据管理-数据模块化
    vue 项目其他规范
    vue路由管理-保留滚动位置功能、按需加载模块名自定义
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/14504476.html
Copyright © 2020-2023  润新知