Shiro 认证 & 盐加密- SSM

本文详细介绍了如何在SSM(Spring、SpringMVC、MyBatis)框架中结合Shiro进行用户认证,并且讲解了盐加密的实现过程。首先,文章列出了Shiro认证的步骤,包括Pom依赖、web.xml配置、自定义Realm以及相关服务层和配置文件的设置。接着,作者提到了盐加密工具类PasswordHelper在用户注册时加密密码的作用,虽然在现有数据下未实际使用。最后,文章展示了不同权限用户登录后的不同页面显示,并提供了数据库脚本和登录、注册界面的示例。
摘要由CSDN通过智能技术生成

Shiro 认证 - SSM

本章目标:
1、shiro认证
2、盐加密

请先参考我的另一篇博客 Shiro 入门使用 ,能更好的理解本章内容,Shiro 入门使用:https://blog.csdn.net/qq_44641053/article/details/102527260

Shiro认证

1、Pom依赖:

			<dependency>
			    <groupId>org.apache.shiro</groupId>
			    <artifactId>shiro-core</artifactId>
			    <version>1.3.2</version>
			</dependency>
			
			<dependency>
			    <groupId>org.apache.shiro</groupId>
			    <artifactId>shiro-web</artifactId>
			    <version>1.3.2</version>
			</dependency>
			
			<dependency>
			    <groupId>org.apache.shiro</groupId>
			    <artifactId>shiro-spring</artifactId>
			    <version>1.3.2</version>
			</dependency>

2、web.xml配置:

			<!-- shiro过滤器定义 -->
			<filter>
			  <filter-name>shiroFilter</filter-name>
			  <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
			  <init-param>
			    <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true则表示由ServletContainer管理 -->
			    <param-name>targetFilterLifecycle</param-name>
			    <param-value>true</param-value>
			  </init-param>
			</filter>
			<filter-mapping>
			  <filter-name>shiroFilter</filter-name>
			  <url-pattern>/*</url-pattern>
			</filter-mapping>

通过 逆向工程 将五张表生成对应的 model、mapper(ShiroUser,ShiroUserMapper,ShiroUserMapper.xml)

3、自定义 Realm

MyRealm.java

package com.dj.ssm.shiro;

import com.dj.ssm.model.ShiroUser;
import com.dj.ssm.model.User;
import com.dj.ssm.service.ShiroUserService;
import com.dj.ssm.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Service;

public class MyRealm extends AuthorizingRealm {
   
    private ShiroUserService shiroUserService;

    public ShiroUserService getShiroUserService() {
   
        return shiroUserService;
    }

    public void setShiroUserService(ShiroUserService shiroUserService) {
   
        this.shiroUserService = shiroUserService;
    }

	/**
     * 授权
     * @param principalCollection
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
   
        return null;
    }

	/**
     * 认证
     * @param token         从jsp传递过来的用户名密码组合成的一个token对象
     * @return
     * @throws AuthenticationException
     *
     * 认证过程:
     * 1.数据源(ini--》数据库)
     * 2.AuthenticationInfo将数据库的用户信息给subject主题做shiro认证的
     *      2.1.需要在当前的realm中调用service来验证,当前用户是否在数据库中存在
     *      2.2.盐加密
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
   
        System.out.println("身份认证...");
        String username = token.getPrincipal().toString();
        String password = token.getCredentials().toString();
        ShiroUser user = shiroUserService.queryByName(username);
//        拿到数据库中的用户信息,放入token凭证中,用于controler进行对比
        AuthenticationInfo info = new SimpleAuthenticationInfo(
                user.getUsername(),
                user.getPassword(),
                ByteSource.Util.bytes(user.getSalt()),
                this.getName()
        );
        return info;
    }
}

ShiroUserMapper和ShiroUserMapper.xml中加入:

		ShiroUser queryByName(String uname);

Mapper 中新增

		<select id="queryByName" resultType="com.dj.ssm.model.ShiroUser" parameterType="java.lang.String">
		  select
		  <include refid="Base_Column_List" />
		  from t_shiro_user
		  where userName = #{userName}
		</select>

Service层

ShiroUserService.java

package com.dj.ssm.service;

import com.dj.ssm.model.ShiroUser;

import java.util.Set;

public interface ShiroUserService {
   
    public Set<String> getRolesByUserId(Integer userId);

    public Set<String> getPersByUserId(Integer userId);

    public ShiroUser queryByName(String userName);

	public int insert(ShiroUser shiroUser);
}

ShiroUserServiceImpl.java

package com.dj.ssm.service.impl;

import com.dj.ssm.mapper.ShiroUserMapper;
import com.dj.ssm.model.ShiroUser;
import com.dj.ssm.service.ShiroUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

@Service("shiroUserService")
public class ShiroUserServiceImpl implements ShiroUserService {
   
    @Autowired
    
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值