Shiro实现注册、登录

一、shiro前提配置

1)添加依赖

<properties>
        <mysql.version>8.0.33</mysql.version>
        <druid.version>1.1.10</druid.version>
        <java.version>1.8</java.version>
        <shiro.version>1.7.1</shiro.version>
        <mybatis.version>3.4.2</mybatis.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-ehcache</artifactId>
            <version>1.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>${druid.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-spring-boot-web-starter</artifactId>
            <version>${shiro.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

2)yml相关配置

#数据库相关配置
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
    url: jdbc:mysql://localhost:3306/shiro?useSSL=false
    username: root
    password: 20001002qql
#shiro相关配置
shiro:
  loginUrl: login.html
  rememberMeManager:
    cookie:
      maxAge: 3600
  userNativeSessionManager: true
  sessionManager:
    sessionIdUrlRewritingEnabled: false
mybatis-plus:
  type-aliases-package: com.zs.shiro.entity
  mapper-locations: classpath*:mapper/*Mapper.xml
logging:
  level:
    com.zs.shiro.dao: debug
二、实现注册

1)创建盐配置类,添加用户时,在serviceImpl中对密码进行盐加密,使用md5进行加密

SaltUtils

package com.zs.shiro.utils;

import java.util.Random;

public class SaltUtils {
    public static String getSlat(int n){
        char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%&*".toCharArray();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            char str = chars[new Random().nextInt(chars.length)];
            sb.append(str);
        }
        return sb.toString();
    }
}

Controller

@RequestMapping("/register")
    public String register(User user){
        Boolean flag = userService.addUser(user);
        if (flag){
            return "redirect:/login.html";
        }
        return "redirect:/error.html";
    }

ServiceImpl

@Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean addUser(User user) {
        //生成盐
        String salt = SaltUtils.getSlat(8);
        user.setSalt(salt);
        //密码加密
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setPassword(md5Hash.toHex());
        return this.save(user);
    }
三、实现登录

1)ShiroConfig配置

@Configuration
public class ShiroConfig {
    @Bean
    public ShiroFilterChainDefinition shiroFilterChainDefinition(){
        DefaultShiroFilterChainDefinition chainDefinition = new DefaultShiroFilterChainDefinition();
        Map<String,String> map = new HashMap<>();
        //登录放行
        map.put("/user/register","anon");
        map.put("/user/test","authc");
        //免cokkie登录
        map.put("/index.html","user");
        chainDefinition.addPathDefinitions(map);
        return chainDefinition;
    }

    @Bean
    public Realm customRealm(){
        CustomRealm customRealm = new CustomRealm();
        //设置hashed凭证匹配器
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //设置md5加密
        credentialsMatcher.setHashAlgorithmName("md5");
        //设置散列次数
        credentialsMatcher.setHashIterations(1024);
        customRealm.setCredentialsMatcher(credentialsMatcher);
        return customRealm;
    }
}

2)Realm配置

public class CustomRealm extends AuthorizingRealm {
    @Autowired
    private UserService userService;

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        /**
         * 根据从数据库查询出来的角色对不同用户进行授权,在Controller方法中添加注解
         * @RequiresRoles(value={"admin",...})  用来判断角色
         * @RequiresPermissions("order:save:*") 用来判断权限字符串
         */
        return null;
    }

    //角色认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String username = token.getPrincipal().toString();
        User user = getUserByUsernameFromMysql(username);
        if (user == null){
            throw new UnknownAccountException("用户不存在");
        }
        return new SimpleAuthenticationInfo(user,user.getPassword(), ByteSource.Util.bytes(user.getSalt()),this.getName());
    }

    private User getUserByUsernameFromMysql(String username) {
        return userService.selectUserByUsername(username);
    }
}

3)Controller代码实现

@RequestMapping("/login")
    public String login(String username,String password,Boolean rememberMe){
        Subject subject = SecurityUtils.getSubject();
        subject.login(new UsernamePasswordToken(username,password,rememberMe));
        return "redirect:/index.html";
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache Shiro是一个强大的安全框架,用于Java应用程序,提供了身份验证、授权和会话管理等功能。在Shiro中,注册过滤器是一个常见的步骤,用于在HTTP请求通过应用程序时应用安全检查。 **如何在Shiro注册过滤器:** 1. **添加依赖**:首先,你需要在项目的pom.xml或build.gradle文件中添加Shiro的依赖。 2. **配置Web过滤器**:在Spring Boot项目中,通常会在WebMvcConfigurer接口的实现类中注册过滤器。例如: ```java @Configuration public class ShiroConfig implements WebMvcConfigurer { @Autowired private SecurityManager securityManager; @Override public void addViewControllers(ViewControllerRegistry registry) { // 其他配置... } @Override public void addFilter(ServletFilterRegistrationBean<?> filterRegistrationBean) { filterRegistrationBean.setFilter(securityManager.createFilterChainResolver().getFilter链名称()); filterRegistrationBean.addUrlPatterns("/*"); filterRegistrationBean.setDispatcherTypes(DispatcherType.REQUEST); } } ``` 这里的`securityManager.createFilterChainResolver().getFilterChain()`会返回Shiro的过滤器链,这个链包含了所有需要的应用过滤器,包括身份验证和授权过滤器。 3. **创建Filter链**:在Shiro的配置中,你需要定义过滤器链,通常使用`HttpSecurity`的`authorizeRequests()`方法来指定哪些URL需要特定的权限。 4. **身份验证过滤器**:Shiro提供了多种身份验证过滤器,如BasicAuthFilter、FormAuthenticationFilter等,你可以根据需求选择并配置。 5. **登录处理**:配置一个登录处理程序(LoginUrlAuthenticationFilter),定义用户登录失败后的重定向URL。 6. **退出处理**:记得配置一个退出过滤器,以便用户在注销时清除会话和权限信息。 **相关问题--:** 1. Shiro的过滤器链是如何组织的? 2. 如何在Shiro中自定义身份验证过滤器? 3. Spring Boot集成Shiro后,如何配置登录和退出流程?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值