栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Shiro——配合Spring

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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"));
	}
}

运行效果:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/726124.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号