Shiro——配合Spring

Shiro——配合Spring,第1张

Shiro——配合Spring

文章目录

依赖Bean配置工具类变化测试

依赖

Spring


    org.springframework
    spring-context
    ${springV}


    org.springframework
    spring-test
    ${springV}

Shiro


    org.apache.shiro
    shiro-core
    1.3.2


    org.apache.shiro
    shiro-ehcache
    1.3.2


    org.apache.shiro
    shiro-spring
    1.3.2

日志


    org.apache.logging.log4j
    log4j-web
    2.17.1


    org.apache.logging.log4j
    log4j-slf4j-impl
    2.17.1

缓存


    org.ehcache
    ehcache
    3.9.0

开发辅助


    junit
    junit
    4.13.2
    test


    org.projectlombok
    lombok
    RELEASE
    compile

Bean配置

    
        
    
    
        
    


    
    


工具类变化

去掉static块

public class ShiroUtil {

	public static Subject login(String username, String password) {
		Subject s = SecurityUtils.getSubject();
		try {
			s.login(new UsernamePasswordToken(username, md5(password)));
		} catch (AuthenticationException e) {
			e.printStackTrace();
		}
		return s;
	}

	private static String md5(String input) {
		return new Md5Hash(input, "manage", 1024).toString();
	}
}
测试

没有多少变化,引入Spring测试

@ContextConfiguration("classpath:spring.xml")
@RunWith(SpringRunner.class)
public class HelloTest {
	@Test
	public void f1() {
		Subject abc = ShiroUtil.login("abc", "123");
		System.out.println("登陆状态" + abc.isAuthenticated());
		System.out.println("有admin角色" + abc.hasRole("admin"));
		System.out.println("有u1权限" + abc.isPermitted("u1"));
	}
}

运行效果:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5721467.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存