zoukankan      html  css  js  c++  java
  • shiro登录认证

    新建maven文件create a simple project

    shiro.ini文件

    [users]
    root=123456
    

    log4j.properties

    log4j.rootLogger=INFO, stdout
    
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m %n
    
    # General Apache libraries
    log4j.logger.org.apache=WARN
    
    # Spring
    log4j.logger.org.springframework=WARN
    
    # Default Shiro logging
    log4j.logger.org.apache.shiro=TRACE
    
    # Disable verbose logging
    log4j.logger.org.apache.shiro.util.ThreadContext=WARN
    log4j.logger.org.apache.shiro.cache.ehcache.EhCache=WARN

    java文件

    import org.apache.shiro.SecurityUtils;
    import org.apache.shiro.authc.AuthenticationException;
    import org.apache.shiro.authc.UsernamePasswordToken;
    import org.apache.shiro.config.IniSecurityManagerFactory;
    import org.apache.shiro.mgt.SecurityManager;
    import org.apache.shiro.subject.Subject;
    import org.apache.shiro.util.Factory;
    
    public class HelleWorld {
    
        public static void main(String[] args) {
            // 读取配置文件,初始化SecurityManager工厂
            Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro.ini");
            // 获取securityManager实例
            SecurityManager securityManager=factory.getInstance();
            // 把securityManager实例绑定到SecurityUtils
            SecurityUtils.setSecurityManager(securityManager);
            // 得到当前执行的用户
            Subject currentUser=SecurityUtils.getSubject();
            // 创建token令牌,用户名/密码
            UsernamePasswordToken token=new UsernamePasswordToken("root", "123456");
            try{
                // 身份认证
                currentUser.login(token);    
                System.out.println("身份认证成功!");
            }catch(AuthenticationException e){
                e.printStackTrace();
                System.out.println("身份认证失败!");
            }
            // 退出
            currentUser.logout();
        }
    }

    结果:

  • 相关阅读:
    Codeforces Round #456 (Div. 2)
    Codeforces Round #455 (Div. 2)
    Codeforces Round #453 (Div. 1)
    Codeforces Round #450 (Div. 2)
    退役了
    退役了
    这个博客不想要了
    Hello!The familiar and strange world.
    真正的退役了。
    bzoj4231: 回忆树
  • 原文地址:https://www.cnblogs.com/yyy1234/p/10424267.html
Copyright © 2011-2022 走看看