hahahahah

dsfsefesfsffsfsfsfsfesfsfsfsfsfsfspackage realm;

?

import java.util.ArrayList;

import java.util.List;

?

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;

import org.apache.commons.lang3.builder.ToStringStyle;

import org.apache.shiro.SecurityUtils;

import org.apache.shiro.authc.AuthenticationException;

import org.apache.shiro.authc.AuthenticationInfo;

import org.apache.shiro.authc.AuthenticationToken;

import org.apache.shiro.authc.SimpleAuthenticationInfo;

import org.apache.shiro.authc.UsernamePasswordToken;

import org.apache.shiro.authz.AuthorizationException;

import org.apache.shiro.authz.AuthorizationInfo;

import org.apache.shiro.authz.SimpleAuthorizationInfo;

import org.apache.shiro.realm.AuthorizingRealm;

import org.apache.shiro.session.Session;

import org.apache.shiro.subject.PrincipalCollection;

import org.apache.shiro.subject.Subject;

import org.springframework.beans.factory.annotation.Autowired;

?

import utils.StrUtils;

?

import com.jxzg.mvc.web.entitys.user.Role;

import com.jxzg.mvc.web.entitys.user.RoleRight;

import com.jxzg.mvc.web.entitys.user.User;

import com.jxzg.mvc.web.service.user.IUserManager;

?

public class MyRealm extends AuthorizingRealm {

?

[email protected]

????private IUserManager userManager;

?

????/**

???? * 为当前登录的Subject授予角色和权限

???? * @see 经测试:本例中该方法的调用时机为用户登录后,被调用

???? */

[email protected]

????protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {

????????// 获取当前登录的用户名,等价于(String)principals.fromRealm(this.getName()).iterator().next()

????????String currentUsername = (String) super.getAvailablePrincipal(principals);

????????List<String> roleList = new ArrayList<String>();

????????List<String> permissionList = new ArrayList<String>();

????????// 从数据库中获取当前登录用户的详细信息

????????User user = userManager.getByUsername(currentUsername);

????????if (null != user) {

????????????// 实体类User中包含有用户角色的实体类信息

????????????if (null != user.getRole()) {

????????????????// 获取当前登录用户的角色

????????????????Role role = user.getRole();

????????????????roleList.add(role.getName());

????????????????//如果是超级管理员直接赋予所有权限

????????????????if(role.getName().equals("admin")){

????????????????????permissionList.add("user");

????????????????????permissionList.add("school");

????????????????}

????????????????

????????????????else{

????????????????????// 实体类Role中包含有角色权限的实体类信息

????????????????????if (null != role.getRights() && role.getRights().size() > 0) {

????????????????????????// 获取权限

????????????????????????for (RoleRight pmss : role.getRights()) {

????????????????????????????if(pmss.isFlag()){

????????????????????????????????if (!StrUtils.isNullOrEmpty(pmss.getRight())) {

????????????????????????????????????permissionList.add(pmss.getRight().getName());

????????????????????????????????}

????????????????????????????}

????????????????????????}

????????????????????}

????????????????}

????????????}

????????} else {

????????????throw new AuthorizationException();

????????}

????????// 为当前用户设置角色和权限

????????SimpleAuthorizationInfo simpleAuthorInfo = new SimpleAuthorizationInfo();

????????simpleAuthorInfo.addRoles(roleList);

????????simpleAuthorInfo.addStringPermissions(permissionList);

????????return simpleAuthorInfo;

????}

?

????/**

???? * 验证当前登录的Subject

???? * @see 经测试:本例中该方法的调用时机为LoginController.login()方法中执行Subject.login()时

???? */

[email protected]

????protected AuthenticationInfo doGetAuthenticationInfo(

????????????AuthenticationToken authcToken) throws AuthenticationException {

????????// 获取基于用户名和密码的令牌

????????// 实际上这个authcToken是从LoginController里面currentUser.login(token)传过来的

????????// 两个token的引用都是一样的

????????UsernamePasswordToken token = (UsernamePasswordToken) authcToken;

????????System.out.println("验证当前Subject时获取到token为"

????????????????+ ReflectionToStringBuilder.toString(token,

????????????????????????ToStringStyle.MULTI_LINE_STYLE));

????????User user = userManager.getByUsername(token.getUsername());

????????if (null != user) {

????????????AuthenticationInfo authcInfo = new SimpleAuthenticationInfo(

????????????????????user.getUserName(), user.getPass(), user.getNickName());

????????????this.setSession("currentUser", user);

????????????return authcInfo;

????????} else {

????????????return null;

????????}

????}

?

????/**

???? * 将一些数据放到ShiroSession中,以便于其它地方使用

???? * @see 比如Controller,使用时直接用HttpSession.getAttribute(key)就可以取到

???? */

????private void setSession(Object key, Object value) {

????????Subject currentUser = SecurityUtils.getSubject();

????????if (null != currentUser) {

????????????Session session = currentUser.getSession();

????????????if (null != session) {

????????????????session.setAttribute(key, value);

????????????}

????????}

????}

?

}

时间: 2024-08-26 16:13:51

hahahahah的相关文章

appium 滑动

前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html   知乎Android客户端登陆:http://www.cnblogs.com/tobecrazy/p/4579631.html appium实现截图和清空EditText:http://www.cnblogs.com/tobecrazy/p/4592405.html Appium 处理滑动 appium 处理滑动的方法是 swipe(i

.Net学习笔记----字符串string的各种方法

端午节前学到了字符串处理,结果过了个节都忘了,今天拿出来重新过一遍,加深印象 随机输入心中想到的一个名字,然后输出它的字符串长度 //随机输入你心中想到的一个名字,然后输出它的字符串长度 Console.WriteLine("请输入你心中想的那个人的名字"); string name = Console.ReadLine(); Console.WriteLine("这个人的名字是{0}", name.Length); Console.ReadKey(); 两=个学员

appium实现截图和清空EditText

前些日子,配置好了appium测试环境,至于环境怎么搭建,参考:http://www.cnblogs.com/tobecrazy/p/4562199.html   知乎Android客户端登陆:http://www.cnblogs.com/tobecrazy/p/4579631.html 在使用appium的过程中,发现一些appium的坑(后边会详说). adb基本命令总结(Android Debug Bridge) adb 是PC和设备连接的桥梁,可以通过adb对devices进行相关操作

appium实现adb命令 截图和清空EditText

原文地址http://www.cnblogs.com/tobecrazy/p/4592405.html 原文地址http://www.cnblogs.com/tobecrazy/ 该博主有很多干货,可以多去研究研究 adb基本命令总结(Android Debug Bridge) adb 是PC和设备连接的桥梁,可以通过adb对devices进行相关操作 adb devices           列出你的devices adb kill-server         杀掉adb服务(如果设备连接

数据库高级对象

1.函数 函数可以传参,也可以有返回值.但是函数只能有一个返回值函数可以包含0-多个参数 声明不带参数的函数(以下都以MySql数据库为例) CREATE FUNCTION 函数名() RETURNS 返回值类型RETURN 返回结果; 如果函数体中有多行语句,需要使用begin end 包裹 CREATE FUNCTION f1() RETURNS VARCHAR(255) BEGIN DELETE FROM tb1 WHERE id=1; RETURN "hahahahah"; E

boost asio库 同步socket连接示例

<pre name="code" class="cpp">/////////////////////////////////////// // Asio同步socket连接示例 // #include <iostream> #include <boost/thread.hpp> #include <boost/asio/io_service.hpp> #include <boost/asio.hpp> us

python学习道路(day8note)(抽象类,类的方法,异常处理,socket编程)

1.#面向对象 #抽象接口 === 抽象类 #就是架构师给你一个架子,你们去写,如果满足不了直接报错 1 #python2 2 print("python2---抽象类".center(20,'#')) 3 import abc #需要在python2中测试,如果直接运行会报错 4 #因为没有调用send方法 5 class Alert(object): 6 '''报警基类''' 7 __metaclass__ = abc.ABCMeta 8 @abc.abstractmethod 9

关于浏览器事件

浏览器事件流是指页面接受事件的顺序: 有事件捕获和事件冒泡: 事件捕获是指从document到html再到body最后到div元素. 事件冒泡是指事件会从div传到body再到html再到最外面的document. 在dom事件流中分为事件捕获,处于目标阶段,事件冒泡. 事件处理程序,也就是绑定事件有html事件处理程序,dom0级事件处理程序,dom2级处理程序,和ie事件处理程序. html处理程序就是在html中绑定事件:<input type="button" name=

AC日记——[Noi2011]阿狸的打字机 bzoj 2434

2434 思路: 构建ac自动机: 抽离fail树: 根据字符串建立主席树: 在线处理询问: 询问x在y中出现多少次,等同于y有多少字母的fail能走到x: 1a,hahahahah: 代码: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define maxn