自定义shiro 的密码验证方式

项目环境spring boot 2.1

首先实现自己的验证类

import com.glodon.security.MD5;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;

public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken authcToken, AuthenticationInfo info) {
        UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

        Object tokenCredentials = encrypt(String.valueOf(token.getPassword()));
        Object accountCredentials = getCredentials(info);
        //将密码加密与系统加密后的密码校验,内容一致就返回true,不一致就返回false
        return equals(tokenCredentials, accountCredentials);
    }

    //将传进来密码加密方法
    private String encrypt(String data) {
        String encodePwd = MD5.toMD5(data);//这里可以选择自己的密码验证方式 比如 md5或者sha256等
        return encodePwd;
    }
}

然后在自定义的realm中引入:

 

public class FrontUserRealm extends AuthorizingRealm {

        @Override
        protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
            SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
            return authorizationInfo;
        }

        /**
         * @Author Adam
         * @Description  主要是用来进行身份认证的,也就是说验证用户输入的账号和密码是否正确
         * @Date 14:06 2018/9/28
         * @Param [token]
         * @return org.apache.shiro.authc.AuthenticationInfo
         **/
        @Override
        protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
                throws AuthenticationException {
            FrontUserAuthService shiroFactory = FrontUserAuthServiceServiceImpl.me();
            UsernamePasswordToken userToken = (UsernamePasswordToken)token;
            FrontUser user = shiroFactory.user(userToken.getUsername());
            ShiroUser shiroUser = shiroFactory.shiroUser(user);
            return shiroFactory.info(shiroUser,user, super.getName());
        }
    /**
     * 设置认证加密方式
     */
    @Override
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        CustomCredentialsMatcher customCredentialsMatcher = new     
        CustomCredentialsMatcher();
        super.setCredentialsMatcher(customCredentialsMatcher);
    }
}

,如此这般就可以用自己的验证方式了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shiro 是一个强大的 Java 安全框架,提供了身份认证、授权、加密等功能。以下是一个简单的 Shiro 权限验证代码示例: 1. 配置 Shiro 安全管理器 ```java // 创建一个 DefaultSecurityManager 对象 DefaultSecurityManager securityManager = new DefaultSecurityManager(); // 设置 Realm securityManager.setRealm(myRealm); // 将 SecurityManager 设置为全局的安全管理器 SecurityUtils.setSecurityManager(securityManager); ``` 2. 创建一个自定义 Realm 类 ```java public class MyRealm extends AuthorizingRealm { // 授权方法 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) { // 获取当前用户 String username = (String) principals.getPrimaryPrincipal(); // 根据用户名查询该用户的角色和权限 Set<String> roles = userService.findRolesByUsername(username); Set<String> permissions = userService.findPermissionsByUsername(username); // 创建 SimpleAuthorizationInfo 对象,并设置角色和权限 SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo(); authorizationInfo.setRoles(roles); authorizationInfo.setStringPermissions(permissions); return authorizationInfo; } // 认证方法 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { // 获取用户名和密码 String username = (String) token.getPrincipal(); String password = new String((char[]) token.getCredentials()); // 查询用户是否存在 User user = userService.findByUsername(username); if (user == null) { throw new UnknownAccountException("用户不存在"); } // 验证密码 if (!passwordEncoder.matches(password, user.getPassword())) { throw new IncorrectCredentialsException("密码错误"); } // 创建 SimpleAuthenticationInfo 对象,并设置用户名和密码 SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(username, password, getName()); return authenticationInfo; } } ``` 3. 在 Controller 中进行权限验证 ```java @Controller public class UserController { @RequiresPermissions("user:list") @GetMapping("/user/list") public String list() { // 用户列表页面 return "user/list"; } @RequiresPermissions("user:add") @GetMapping("/user/add") public String add() { // 添加用户页面 return "user/add"; } @RequiresPermissions("user:edit") @GetMapping("/user/edit") public String edit() { // 编辑用户页面 return "user/edit"; } } ``` 在上述代码中,@RequiresPermissions 注解表示该方法需要进行权限验证。如果当前用户拥有对应的权限,则可以访问该方法;否则将抛出 UnauthorizedException 异常。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值