Shiro

Quickstart.java

所有的环境下,都可以通过这种方式获取当前用户:

Subject currentUser = SecurityUtils.getSubject();

在单应用系统中,调用getSubject()会返回一个Subject,它是位于应用程序中特定位置的用户信息;在服务器中运行的情况下(比如web应用),getSubject会返回一个位于当前线程或请求中的用户信息。 现在你已经得到了Subject对象,如果你想得到应用中用户当前Session的其他参数,可以这样获取Session对象:

Session session = currentUser.getSession();

session.setAttribute( "someKey", "aValue" );

这个Session对象是Shiro中特有的对象,它和我们经常使用的HttpSession非常相似,但还提供了额外的东西,其中与HttpSession最大的不同就是Shiro中的Session不依赖HTTP环境(换句话说,可以在非HTTP 容器下运行)。

如果将Shiro部署在web应用程序中,那么这个Session就是基于HttpSession的。但是像QuickStart示例那样,在非web环境下使用,Shiro则默认使用EnterpriseSessionManagment。也就是说,不论在应用中的任何一层使用同样的API,却不需要考虑部署环境,这一优点为应用打开一个全新的世界,因为应用中要获取Session对象再也不用依赖于HttpSession或者EJB的会话Bean。而且任何客户端技术都可以共享session 数据。

现在你可以得到当前Subject和它的Session对象。那么我们如何验证比如角色和权限这些东西呢?

很简单,可以通过已得到的user对象进行验证。Subject对象代表当前用户,但是,谁才是当前用户呢?他们可是匿名用户啊。也就是说,必须登录才能获取到当前用户。没问题,这样就可以搞定:

if ( !currentUser.isAuthenticated() ) {

UsernamePasswordToken token = new UsernamePasswordToken("lonestarr", "vespa");

token.setRememberMe(true);

currentUser.login(token);

}

那登录失败了可以通过捕获各类异常,根据不同类型的异常做出不同的处理:

try {

currentUser.login( token );

//if no exception, that's it, we're done!

} catch ( UnknownAccountException uae ) {

//username wasn't in the system, show them an error message?

} catch ( IncorrectCredentialsException ice ) {

//password didn't match, try again?

} catch ( LockedAccountException lae ) {

//account for that username is locked - can't login. Show them a message?

}

... more types exceptions to check if you want ...

} catch ( AuthenticationException ae ) {

//unexpected condition - error?

}

提示:最安全的做法是将登录失败的消息告知用户,你总不会帮助攻击者入侵你的系统吧!

OK,现在已经拥有一个登录用户了,我们还能做点儿什么呢?

比方说,他们是谁:

//print their identifying principal (in this case, a username):

log.info( "User [" + currentUser.getPrincipal() + "] logged in successfully." );

也可以判断用户是否拥有特定的角色:

if ( currentUser.hasRole( "schwartz" ) ) {

log.info("May the Schwartz be with you!" );

} else {

log.info( "Hello, mere mortal." );

}

还可以判断用户是否对特定某实体有操作权限:

if ( currentUser.isPermitted( "lightsaber:weild" ) ) {

log.info("You may use a lightsaber ring. Use it wisely.");

} else {

log.info("Sorry, lightsaber rings are for schwartz masters only.");

}

当然,还可以进行功能强大的实例级别的权限验证。通过它可以判断用户是否有访问特定类型实例的权限:

if ( currentUser.isPermitted( "winnebago:drive:eagle5" ) ) {

log.info("You are permitted to 'drive' the 'winnebago' with license plate (id) 'eagle5'. " +"Here are the keys - have fun!");

} else {

log.info("Sorry, you aren't allowed to drive the 'eagle5' winnebago!");

}

最后,当用户使用完毕,还可以退出应用。

currentUser.logout(); //removes all identifying information and invalidates their session too.

这些就是使用Apache Shiro开发应用的核心了,当然,Apache Shiro已将将很多复杂的东西封装在内部了,但是现在它就是这么简单。

你会有疑问吧,用户登录时,谁负责把用户信息(用户名、密码、角色、权限等)取出来,还有运行时,谁负责安全认证呢?当然由你决定了啊。通过将一个实现了Shiro中的Realm的Reaml配置到Shiro中即可。

Shiro 主要有四个组件

  1. SecurityManager

    典型的 Facade,Shiro 通过它对外提供安全管理的各种服务。

  2. Authenticator

    对“Who are you ?”进行核实。通常涉及用户名和密码。

    这个组件负责收集 principals 和 credentials,并将它们提交给应用系统。如果提交的 credentials 跟应用系统中提供的 credentials 吻合,就能够继续访问,否则需要重新提交 principals 和 credentials,或者直接终止访问。

  3. Authorizer

    身份份验证通过后,由这个组件对登录人员进行访问控制的筛查,比如“who can do what”, 或者“who can do which actions”。Shiro 采用“基于 Realm”的方法,即用户(又称 Subject)、用户组、角色和 permission 的聚合体。

  4. Session Manager

    这个组件保证了异构客户端的访问,配置简单。它是基于 POJO/J2SE 的,不跟任何的客户端或者协议绑定。

Shiro 的认证和签权可以通过 JDBC、LDAP 或者 Active Directory 来访问数据库、目录服务器或者 Active Directory 中的人员以及认证 / 签权信息。SessionManager 通过会话 DAO 可以将会话保存在 cache 中,或者固化到数据库或文件系统中。

使用shiro认证分为以下几个步骤:

http://blog.csdn.net/leonzhouwei/article/details/8272104

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值