搭建Shiro开发环境
Apache Shiro共有以下几个Jar包:
- Apache Shiro :: Core —— 核心包
- Apache Shiro :: Web —— 添加Web支持
- Apache Shiro :: Support :: Spring —— 集成Spring
- Apache Shiro :: Support :: EHCache —— 集成EHCache,用于缓存权限
- Apache Shiro :: Support :: Guice —— 集成Guice(类似Spring,是Google的轻量级依赖注入框架)
- Apache Shiro :: Support :: CAS —— CAS 单点登陆
- Apache Shiro Integration For Grails —— 整合Grails
根据自己的需要添加所需的jar即可。
程序运行流程大致如下:
编写程序:
添加依赖
<!--shiro核心模块--> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>${shiro.version}</version> </dependency> <!--shiro需要日志依赖--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.2.3</version> </dependency>
编写代码
public class HelloShiro { private static Logger logger = LoggerFactory.getLogger(HelloShiro.class); @Test public void hello() { //1.构建SecurityManager环境 DefaultSecurityManager sm = new DefaultSecurityManager(); //2.配置安全信息源 SimpleAccountRealm realm = new SimpleAccountRealm(); realm.addAccount("Feathers", "123456", "admin"); sm.setRealm(realm); //3.绑定SecurityManager到当前运行环境中,在当前运行环境中,SecurityManager是单例的 SecurityUtils.setSecurityManager(sm); //4.获取主体(用户),提交认证请求 Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("Feathers", "123456"); try { subject.login(token); } catch (UnknownAccountException e) { logger.error("找不到账户", e); } catch (IncorrectCredentialsException e) { logger.error("密码错误", e); } catch (LockedAccountException e) { logger.error("账户已经被锁定", e); } //5.获取认证结果 logger.info("usAuthenticated: {}", subject.isAuthenticated()); //6.权限验证 try { subject.checkRoles("admin"); } catch (UnauthenticatedException e) { logger.error("对不起,请您先登陆", e); } catch (UnauthorizedException e) { logger.error("对不起,您没有该权限"); } } }
代码详见模块:hello-world
相关文章
- 基于-SLF4J-MDC-机制的日志链路追踪配置属性
ums: # ================ 基于 SLF4J MDC 机制的日志链路追踪配置属性 ================ mdc: # 是否支持基于 SLF4J MDC
- ajax-跨域访问
ajax 跨域访问 <!DOCTYPE html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head>
- 给第三方登录时用的数据库表-user_connection-与-auth_token-添加-redis-cache
spring: # 设置缓存为 Redis cache: type: redis # redis redis: host: 192.168.88.88 port
- Java动态代理
Jdk动态代理 通过InvocationHandler和Proxy针对实现了接口的类进行动态代理,即必须有相应的接口 应用 public class TestProxy { public
- Java读取classpath中的文件
public void init() { try { //URL url = Thread.currentThread().getContextClassLo
随机推荐
- 基于-SLF4J-MDC-机制的日志链路追踪配置属性
ums: # ================ 基于 SLF4J MDC 机制的日志链路追踪配置属性 ================ mdc: # 是否支持基于 SLF4J MDC
- ajax-跨域访问
ajax 跨域访问 <!DOCTYPE html> <html xmlns:th="http://www.w3.org/1999/xhtml"> <head>
- 给第三方登录时用的数据库表-user_connection-与-auth_token-添加-redis-cache
spring: # 设置缓存为 Redis cache: type: redis # redis redis: host: 192.168.88.88 port
- Java动态代理
Jdk动态代理 通过InvocationHandler和Proxy针对实现了接口的类进行动态代理,即必须有相应的接口 应用 public class TestProxy { public
- Java读取classpath中的文件
public void init() { try { //URL url = Thread.currentThread().getContextClassLo