shiro的ssm集成和简单的开发尝试
配置web.xml
<!-- 配置shiro的集成开始 -->
<filter>
<filter-name>shiroFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<!-- 这里面的shiroFilter必须和application-shiro.xml里面的
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" >id 一样 -->
<param-name>targetBeanName</param-name>
<param-value>shiroFilter</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>shiroFilter</filter-name>
<servlet-name>springmvc</servlet-name>
</filter-mapping> <!-- 配置shiro的集成结束 -->
创建application-shiro.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 声明凭证匹配器 -->
<bean id="credentialsMatcher" class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
<property name="hashAlgorithmName" value="md5"></property>
<property name="hashIterations" value="2"></property>
</bean> <!-- 声明userRealm -->
<bean id="userRealm" class="com.sxt.realm.UserRealm">
<!-- 注入凭证匹配器 -->
<property name="credentialsMatcher" ref="credentialsMatcher"></property>
</bean> <!-- 配置SecurityManager -->
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
<!-- 注入realm -->
<property name="realm" ref="userRealm"></property>
</bean> <!-- 配置shiro的过滤器 这里面的id必须和web.xml里面的配置一样 -->
<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean" >
<!-- 注入安全管理器 -->
<property name="securityManager" ref="securityManager"></property>
<!-- 注入未登陆的跳转页面 默认的是webapp/login.jsp-->
<property name="loginUrl" value="/index.jsp"></property>
<!-- 注入未授权的访问页面 -->
<property name="unauthorizedUrl" value="/unauthorized.jsp"></property>
<!-- 配置过滤器链 -->
<property name="filterChainDefinitions">
<value>
<!-- 放行index.jsp -->
/index.jsp*=anon
<!-- 放行跳转到登陆页面的路径 -->
/login/toLogin*=anon
<!-- 放行登陆的请求 -->
/login/login*=anon
<!-- 设置登出的路径 -->
/login/logout*=logout
<!-- 设置其它路径全部拦截 -->
/**=authc
</value>
</property> </bean> </beans>
com.sxt.realm.UserRealm 类
public class UserRealm extends AuthorizingRealm { @Autowired
private UserService userService;
@Autowired
private RoleService roleService;
@Autowired
private PermissionService permissionService; @Override
public String getName() {
return this.getClass().getSimpleName();
} /**
* 认证
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = token.getPrincipal().toString();
// 根据用户名查询用户
User user = this.userService.queryUserByUserName(username);
if (null != user) {
//查询角色
List<String> roles = this.roleService.queryRolesByUserId(user.getUserid());
//查询权限
List<String> permissions = this.permissionService.queryPermissionByUserId(user.getUserid());
//构造ActiverUser
ActivierUser activierUser=new ActivierUser(user, roles, permissions);
//创建盐
ByteSource credentialsSalt=ByteSource.Util.bytes(user.getUsername()+user.getAddress());
SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(activierUser, user.getUserpwd(), credentialsSalt, this.getName());
return info;
} else {
return null;
}
} /**
* 授权
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
ActivierUser activierUser = (ActivierUser) principals.getPrimaryPrincipal();
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo(); List<String> roles = activierUser.getRoles();
List<String> permissions = activierUser.getPermissions();
if(null!=roles&&roles.size()>0) {
info.addRoles(roles);
}
if(null!=permissions&&permissions.size()>0) {
info.addStringPermissions(permissions);
}
return info;
} }
User权限和角色类集合
public class ActivierUser { private User user;
private List<String> roles; private List<String> permissions; public ActivierUser() {
// TODO Auto-generated constructor stub
} public ActivierUser(User user, List<String> roles, List<String> permissions) {
super();
this.user = user;
this.roles = roles;
this.permissions = permissions;
}
UserRealm 类的使用
public class UserRealm extends AuthorizingRealm { @Autowired
private UserService userService;
@Autowired
private RoleService roleService;
@Autowired
private PermissionService permissionService; @Override
public String getName() {
return this.getClass().getSimpleName();
} /**
* 认证
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
String username = token.getPrincipal().toString();
System.out.println(token.getPrincipal()+"---账号名");
System.out.println(token.getCredentials()+"--密码");
// 根据用户名查询用户
User user = this.userService.queryUserByUserName(username);
if (null != user) {
//查询角色
List<String> roles = this.roleService.queryRolesByUserId(user.getUserid());
//查询权限
List<String> permissions = this.permissionService.queryPermissionByUserId(user.getUserid());
//构造ActiverUser
ActivierUser activierUser=new ActivierUser(user, roles, permissions);
//创建盐
ByteSource credentialsSalt=ByteSource.Util.bytes(user.getUsername()+user.getAddress());
SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(activierUser, user.getUserpwd(), credentialsSalt, this.getName());
return info;
} else {
return null;
}
} /**
* 授权
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
ActivierUser activierUser = (ActivierUser) principals.getPrimaryPrincipal();
SimpleAuthorizationInfo info=new SimpleAuthorizationInfo(); List<String> roles = activierUser.getRoles();
List<String> permissions = activierUser.getPermissions();
if(null!=roles&&roles.size()>0) {
info.addRoles(roles);
}
if(null!=permissions&&permissions.size()>0) {
info.addStringPermissions(permissions);
}
return info;
} }
登录请求
@RequestMapping("login")
@Controller
public class LoginController { /**
* 跳转到登陆页面
*/
@RequestMapping("toLogin")
public String toLogin() {
return "login";
} /**
* 做登陆
*/
@RequestMapping("login")
public String login(String username,String pwd,HttpSession session) {
//得到主体
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token=new UsernamePasswordToken(username, pwd);
try {
subject.login(token);
System.out.println("登陆成功");
ActivierUser activierUser = (ActivierUser) subject.getPrincipal();
System.out.println(subject.getPrincipal().toString()+"222");
session.setAttribute("user", activierUser.getUser());
return "redirect:/user/toUserManager.action";
} catch (AuthenticationException e) {
e.printStackTrace();
return "redirect:/index.jsp";
}
} }
页面调用
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<shiro:hasPermission name="user:query">
<h1><a href="user/query.action">查询用户</a></h1>
</shiro:hasPermission>
<shiro:hasPermission name="user:add">
<h1><a href="user/add.action">添加用户</a></h1>
</shiro:hasPermission>
<shiro:hasPermission name="user:update">
<h1><a href="user/update.action">修改用户</a></h1>
</shiro:hasPermission>
<shiro:hasPermission name="user:delete">
<h1><a href="user/delete.action">删除用户</a></h1>
</shiro:hasPermission>
<shiro:hasPermission name="user:export">
<h1><a href="user/export.action">导出用户</a></h1>
</shiro:hasPermission>
</body>
</html>
详细请看git
shiro的ssm集成和简单的开发尝试的更多相关文章
- [Shiro] - shiro之SSM中的使用
在学习shiro的途中,在github发现了一个开源项目,所需的控件刚好是自己要学习的方向. 虽然还要学习完ssm的shiro与springboot的shiro,以及接下来的种种控件和类库,但学习这个 ...
- ssm集成redis
身在一个传统的IT公司,接触的新技术比较少,打算年后跳槽,所以抽空学了一下redis. 简单的redis测试,咱们这边就不讲了,现在主要讲讲ssm集成redis的过程,因为现在项目用的就是ssm的框架 ...
- 基于SSM的Java Web应用开发原理初探
SSM开发Web的框架已经很成熟了,成熟得以至于有点落后了.虽然如今是SOA架构大行其道,微服务铺天盖地的时代,不过因为仍有大量的企业开发依赖于SSM,本文简单对基于SSM的Java开发做一快速入门, ...
- Java基于ssm框架的restful应用开发
Java基于ssm框架的restful应用开发 好几年都没写过java的应用了,这里记录下使用java ssm框架.jwt如何进行rest应用开发,文中会涉及到全局异常拦截处理.jwt校验.token ...
- SpringMVC笔记——SSM框架搭建简单实例
落叶枫桥 博客园 首页 新随笔 联系 订阅 管理 SpringMVC笔记——SSM框架搭建简单实例 简介 Spring+SpringMVC+MyBatis框架(SSM)是比较热门的中小型企业级项目开发 ...
- 【Shiro】Apache Shiro架构之集成web
Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shi ...
- Spring Boot 2.X(二):集成 MyBatis 数据层开发
MyBatis 简介 概述 MyBatis 是一款优秀的持久层框架,支持定制化 SQL.存储过程以及高级映射.它采用面向对象编程的方式对数据库进行 CRUD 的操作,使程序中对关系数据库的操作更方便简 ...
- SSM集成
SSM集成 Spring和各个框架的整合 Spring目前是JavaWeb开发中最终的框架,提供一站式服务,可以其他各个框架整合集成 Spring整合方案 SSH Ssh是早期的一种整 ...
- SSM集成FastJson
FastJson Json数据格式回顾 什么是json JSON:(JavaScript Object Notation, JS 对象简谱) 是一种轻量级的数据交换格式.它基于 ECMAScript( ...
随机推荐
- tr标签使用hover的box-shadow效果不生效
先说问题: 这是大致的HTML结构 <table cellpadding="0" cellspacing="0"> <thead> &l ...
- 过滤idea一些不需要的文件和文件夹的显示,在使用svn的时候可以很方便的过滤不需要提交的文件
*.classpath;*.gitignore;*.hprof;*.idea;*.iml;*.lst;*.project;*.pyc;*.pyo;*.rbc;*.settings;*.sh;*.yar ...
- cmake安装jsoncpp
cd jsoncpp- mkdir -p build/debug cd build/debug cmake -DCMAKE_BUILD_TYPE=release -DBUILD_STATIC_LIBS ...
- Go中的数组切片的使用总结
代码示例 package main import "fmt" func main(){ fmt.Println("Hello, world") // 定义数组的 ...
- CSS页面定制代码+动漫人物设计
右下角的小人物(我蛮喜欢的) 把下面这段代码粘贴到设置里页脚代码处 在第六行的model左右的名字可选,我这个是叫z16 然后这里有别人的一篇博客有其他名字https://blog.csdn.net/ ...
- 最小点覆盖(König定理)
König定理是一个二分图中很重要的定理,它的意思是,一个二分图中的最大匹配数等于这个图中的最小点覆盖数.如果你还不知道什么是最小点覆盖,我也在这里说一下:假如选了一个点就相当于覆盖了以它为端点的所有 ...
- spring表达式语言
使用文本表达式 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http: ...
- dbcp数据源连接池
一.数据源连接池 我们之前利用jdbc连接数据库,每次都要创建连接对象,销毁连接对象,如果并发访问量比较大,这样肯定比较辣 浪费数据库的效率,我们可以像之前mybatis中缓存查询到的数据一样,可以把 ...
- 谈谈Spring bean的生命周期(一)
简介 本片文章主要讲Spring IOC容器中 bean 的生命周期 Spring bean 生命周期 Spring 中bean的声明周期 可以分为如下4个阶段: 实例化阶段--Instantiati ...
- 安装stanfordcorenlp成功,import stanfordcorenlp失败,出现错误:importerror-no-module-named-psutil
1.问题描述 安装stanfordcorenlp成功,import stanfordcorenlp失败,pycharm中输入import stanfordcorenlp,然后运行,出现错误:impor ...