栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

简单使用Shiro登录验证

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

简单使用Shiro登录验证

shiro 一、概述 1.shiro简介
  • Apache Shiro是一个Java安全权限框架
  • Shiro不仅可以用在JavaSE环境,也可以用在JavaEE环境
  • Shiro可以完成,认证,授权,加密,会话管理,Web集成,缓存等。
2.快速入门(SpringBoot+mybatisPlus集成)

实现登录拦截

1、导入依赖



    org.springframework.boot
    spring-boot-starter-web



    org.springframework.boot
    spring-boot-starter



    org.projectlombok
    lombok
    true



    org.springframework.boot
    spring-boot-starter-test
    test



    mysql
    mysql-connector-java



    log4j
    log4j
    1.2.17



    com.alibaba
    druid
    1.1.12


    com.baomidou
    mybatis-plus-boot-starter
    3.4.2


    com.baomidou
    mybatis-plus-core
    3.4.2
    compile



    org.apache.shiro
    shiro-spring
    1.4.2


    org.mybatis
    mybatis-spring
    2.0.6
    compile




    org.thymeleaf
    thymeleaf-spring5


    org.thymeleaf.extras
    thymeleaf-extras-java8time

2、编写Shiro配置文件:ShiroConfig

@Configuration
public class ShiroConfig {

//ShiroFilterFactoryBean
@Bean
public ShiroFilterFactoryBean factoryBean(@Qualifier("securityManager") DefaultWebSecurityManager securityManager){
    ShiroFilterFactoryBean factoryBean=new ShiroFilterFactoryBean();
    //将security放入bean中
    factoryBean.setSecurityManager(securityManager);
    
    //拦截页面
    Map filterMap=new linkedHashMap();
    filterMap.put("/user/login","anon");
    filterMap.put("/user/add","authc");
    filterMap.put("/user/update","authc");
    //使用通用符
//        filterMap.put("/user
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
    return null;
}


@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

    //获取当前用户名/账号
    UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;
    String username = token.getUsername();
    //连接数据库,通过用户名得到密码
    Map map=new HashMap<>();
    map.put("username",username);
    List users = userMapper.selectByMap(map);
    UserShiro userShiro=null;
    for (UserShiro user : users) {
        userShiro=user;
    }
    //得到密码
    String password=userShiro.getPassword();
    //密码认证,shiro做
    return new SimpleAuthenticationInfo("",password,"");
}
}

4、登录时需要将账号密码放入token中

@RequestMapping("/user/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 "user/login";
    }catch (UnknownAccountException e){
        //用户名不存在
        model.addAttribute("msg","用户名不存在");
        return "user/login";
    }catch (IncorrectCredentialsException e){
        //密码错误
        model.addAttribute("msg","密码错误");
        return "index";
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/328059.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号