首页 > 其他分享 >org.apache.shiro.authc.UsernamePasswordToken

org.apache.shiro.authc.UsernamePasswordToken

时间:2024-08-07 17:17:03浏览次数:15  
标签:1.4 java UsernamePasswordToken jar apache org shiro

  • 异常
2020-02-20 14:31:44.490  WARN 12388 --- [nio-8091-exec-5] o.a.shiro.authc.AbstractAuthenticator    : Authentication failed for token submission [org.apache.shiro.authc.UsernamePasswordToken - null, rememberMe=false (0:0:0:0:0:0:0:1)].  Possible unexpected error? (Typical or expected login exceptions should extend from AuthenticationException).

java.lang.NullPointerException: null
	at java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936) ~[na:1.8.0_162]
	at org.apache.shiro.util.SoftHashMap.get(SoftHashMap.java:148) ~[shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.cache.MapCache.get(MapCache.java:56) ~[shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.realm.AuthenticatingRealm.getCachedAuthenticationInfo(AuthenticatingRealm.java:488) ~[shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.realm.AuthenticatingRealm.getAuthenticationInfo(AuthenticatingRealm.java:568) ~[shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doSingleRealmAuthentication(ModularRealmAuthenticator.java:180) ~[shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.authc.pam.ModularRealmAuthenticator.doAuthenticate(ModularRealmAuthenticator.java:267) ~[shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.authc.AbstractAuthenticator.authenticate(AbstractAuthenticator.java:198) ~[shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.mgt.AuthenticatingSecurityManager.authenticate(AuthenticatingSecurityManager.java:106) [shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.mgt.DefaultSecurityManager.login(DefaultSecurityManager.java:275) [shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.subject.support.DelegatingSubject.login(DelegatingSubject.java:260) [shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.filter.authc.AuthenticatingFilter.executeLogin(AuthenticatingFilter.java:53) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.filter.authc.FormAuthenticationFilter.onAccessDenied(FormAuthenticationFilter.java:154) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.filter.AccessControlFilter.onAccessDenied(AccessControlFilter.java:133) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.filter.AccessControlFilter.onPreHandle(AccessControlFilter.java:162) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.filter.PathMatchingFilter.isFilterChainContinued(PathMatchingFilter.java:203) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.filter.PathMatchingFilter.preHandle(PathMatchingFilter.java:178) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:131) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:61) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.servlet.AdviceFilter.executeChain(AdviceFilter.java:108) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.servlet.AdviceFilter.doFilterInternal(AdviceFilter.java:137) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.servlet.ProxiedFilterChain.doFilter(ProxiedFilterChain.java:66) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) [shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) [shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:387) [shiro-core-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) [shiro-web-1.4.1.jar:1.4.1]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at com.alibaba.druid.support.http.WebStatFilter.doFilter(WebStatFilter.java:123) [druid-1.1.9.jar:1.1.9]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) [spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) [spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) [spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119) [spring-web-5.2.0.RELEASE.jar:5.2.0.RELEASE]
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_162]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_162]
	at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-9.0.27.jar:9.0.27]
	at java.lang.Thread.run(Thread.java:748) [na:1.8.0_162]
  • realm
@Slf4j
public class UserRealm extends AuthorizingRealm {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserRoleMapper userRoleMapper;
    @Resource
    private RolePermissionMapper rolePermissionMapper;

    @Override   // 授权
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // 获取登录的用户名
        String account = (String) SecurityUtils.getSubject().getPrincipal();
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<String> roles = userRoleMapper.findByRoles(account);  // 获取到的roles
        info.setRoles(roles);   //添加角色
        roles.forEach((role) ->{
            info.setStringPermissions(rolePermissionMapper.findByPermissions(role));    // 角色 授权
        });
        return info;
    }

    @Override   // 认证
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        if (token.getPrincipal() == null){
            throw new LoginException(ExceptionEnum.USERNAME_EMPTY); //用户名不能为空
        }
        String account =  token.getPrincipal().toString(); // 获取用户名
        User user = userMapper.findByAccount(account);   // 获取当前用户名所对应的密码
        if (user == null){
            throw new LoginException(ExceptionEnum.USERNAME_ERROR);  // 账号不存在
        }
//        else if (!user.getPassword().equals(new String((char[]) token.getCredentials()))){
//            throw new LoginException(ExceptionEnum.PASSWORD_ERROR);  // 密码错误
//        }else if (user.getState() == 0){
//            throw new LoginException(ExceptionEnum.ACCOUNT_NOT_ACTIVATED); //账号未激活
//        }else if (user.getState() == 2){
//            throw new LoginException(ExceptionEnum.ACCOUNT_LOCKED);  //账号已被锁定
//        }
        return new SimpleAuthenticationInfo(
                user.getAccount(),user.getPassword(),getName()
        );
    }
}
  • matcher
@Slf4j
public class UserMatcher extends SimpleCredentialsMatcher {
    //
    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken usernamePasswordToken = (UsernamePasswordToken) token;
        log.info("token={}",usernamePasswordToken);
        String pwd = encrypt(String.valueOf(usernamePasswordToken.getPassword()),String.valueOf(usernamePasswordToken.getUsername()));
        String mysqlpwd = (String) info.getCredentials();
        return this.equals(pwd, mysqlpwd);
    }
    private String encrypt(String data,String salt){
        log.info("密码={}",new SimpleHash("MD5",data, ByteSource.Util.bytes(salt),1024).toBase64());
        return new SimpleHash("MD5",data, ByteSource.Util.bytes(salt),1024).toBase64();
    }
  • controller
@Controller
@Slf4j




public class LoginController {
    @PostMapping("/login")  //登录页面
    public String login(String account, String password, Model model) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(account, password);
        try {
            // 登录
            subject.login(token);
        } catch (LoginException e) {
            model.addAttribute("msg", e.getLocalizedMessage());
            return "login";
        }
        if (subject.isAuthenticated()) {
            return "redirect:/index";   //重定向至主页面
        } else {
            token.clear();
            return "login";
        }
    }
}

CSDN 搬家

标签:1.4,java,UsernamePasswordToken,jar,apache,org,shiro
From: https://www.cnblogs.com/antordragon/p/18347471

相关文章

  • 2.3.6版本发布!Apache SeaTunnel Zeta引擎迎来新架构!
    ApacheSeaTunnel2.3.6版本于近日正式发布,社区期待的SeaTunnelZetaMaster/Worker新架构、事件通知机制、支持动态编译的transform等新功能和新能力在这次版本中都有了全面的更新,并添加了首个向量数据库Milvus。此外,本版本还进行了一些基础性的Bug修复和文档修复等,欢迎尝......
  • B站基于Apache DolphinScheduler的一站式大数据集群管理平台(BMR)初窥
    一、背景大数据服务是数据平台建设的基座,随着B站业务的快速发展,其大数据的规模和复杂度也突飞猛进,技术的追求也同样不会有止境。B站一站式大数据集群管理平台(BMR),在千呼万唤中孕育而生。本文简单介绍BMR的由来、面临的主要矛盾以及如何在变化中求得生存与发展。下图是截至2024年......
  • Hadoop3.4.0跑wordcount程序报错:org.apache.hadoop.mapreduce.v2.app.MRAppMaster
    部署完Hadoop3.4.0HA后跑wordcount程序报错,在日志文件里 http://rsnode:8042/logs/userlogs 里看到报错日志说不能加载主类 org.apache.hadoop.mapreduce.v2.app.MRAppMaster网上给的办法大多都是让执行hadoopclasspath然后把那一长串配置到 mapred-site.xml。如图 ......
  • Apache 中的新零日漏洞允许远程代码执行
    ApacheOFBiz开源企业资源规划(ERP)系统中披露了一个新的零日预认证远程代码执行漏洞,该漏洞可能允许威胁行为者在受影响的实例上实现远程代码执行。该漏洞编号为CVE-2024-38856,CVSS评分为9.8(满分10.0)。该漏洞会影响18.12.15之前的ApacheOFBiz版本。发现并报告该漏洞......
  • Spring Boot(八十五):集成Apache PDFBox对pdf文件进行操作
    1 ApachePDFBox简介ApachePDFBox是一个强大的Java库,它可以用来读取、修改和创建PDF文件。在本教程中,我们将介绍如何在SpringBoot项目中集成PDFBox来实现PDF文件的读取与导出功能。官网地址:ApachePDFBox|AJavaPDFLibrary2代码示例创建一个springboot项目。2.1......
  • Goby漏洞发布 | CVE-2024-38856 Apache OFbiz /ProgramExport 命令执行漏洞【已复现】
    漏洞名称:ApacheOFbiz/ProgramExport命令执行漏洞(CVE-2024-38856)EnglishName:ApacheOFbiz/ProgramExportCommandExecutionVulnerability(CVE-2024-38856)CVSScore:9.0漏洞描述:ApacheOFBiz是一个电子商务平台,用于构建大中型企业级、跨平台、跨数据库、跨应用服务器的......
  • Apache OFBiz 授权不当致远程代码执行漏洞(CVE-2024-38856)
    0x01产品简介ApacheOFBiz是一个电子商务平台,用于构建大中型企业级、跨平台、跨数据库、跨应用服务器的多层、分布式电子商务类应用系统。是美国阿帕奇(Apache)基金会的一套企业资源计划(ERP)系统。该系统提供了一整套基于Java的Web应用程序组件和工具。0x02漏洞概述2024年8月......
  • Apache Flink开发时选择Java还是Scala作为编程语言
    在ApacheFlink的开发过程中,选择Java还是Scala作为编程语言是一个重要的决策点。这两种语言各有其独特的优势和特点,适合不同的开发场景和需求。以下是对这一选择的详细探讨,旨在帮助开发者更好地理解并做出合理的选择。一、ApacheFlink简介ApacheFlink是一个开源的分布式......
  • 漏洞复现:Apache solr
    目录漏洞简述环境搭建漏洞复现漏洞检测漏洞修复漏洞简述ApacheSolr是一个开源的搜索服务,使用Java编写、运行在Servlet容器的一个独立的全文搜索服务器,是ApacheLucene项目的开源企业搜索平台。该漏洞是由于没有对输入的内容进行校验,攻击者可利用该漏洞在未授权的情......
  • Maven项目报错:failed to execute goal org.apache.maven.plugins:maven-compiler-plug
    创建了一个maven项目,然后在编译时运行错误:“failedtoexecutegoalorg.apache.maven.plugins:maven-compiler-plugin:3.13.0:compile(default-compile)onprojectforum:thepluginorg.apache.maven.plugins:maven-compiler-plugin:3.13.0requiresmavenversion3.6.3-......