shiro的入门实例-shiro于spring的整合
shiro是一款java安全框架、简单而且可以满足实际的工作需要
第一步、导入maven依赖
<!-- shiro -->
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>${org.apache.shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>${org.apache.shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>${org.apache.shiro.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>${org.apache.shiro.version}</version>
</dependency>
第二步、在项目中定义shiro的过滤器(shiro的实现主要是通过filter实现)
<!-- Shiro Security filter --> <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> </filter> <filter-mapping> <filter-name>shiroFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping>
第三步、创建一个Realm
public class UserRealm extends AuthorizingRealm {
@Autowired
private UserBiz biz;
//验证用户信息,认证的实现
@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
String userno = (String) authenticationToken.getPrincipal();
String password = new String((char[]) authenticationToken.getCredentials());
Result<RcUser> result = biz.login(userno, password);
if (result.isStatus()) {
Session session = SecurityUtils.getSubject().getSession();
session.setAttribute(Constants.Token.RONCOO, userno);
RcUser user = result.getResultData();
return new SimpleAuthenticationInfo(user.getUserNo(), user.getPassword(), getName());
}
return null;
}
//验证用户的权限,实现认证
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
String userno = (String) principals.getPrimaryPrincipal();
Result<RcUser> result = biz.queryByUserNo(userno);
if(result.isStatus()){
Result<List<RcRole>> resultRole = biz.queryRoles(result.getResultData().getId());
if(resultRole.isStatus()){
//获取角色
HashSet<String> roles = new HashSet<String>();
for (RcRole rcRole : resultRole.getResultData()) {
roles.add(rcRole.getRoleValue());
}
System.out.println("角色:"+roles);
authorizationInfo.setRoles(roles);
//获取权限
Result<List<RcPermission>> resultPermission = biz.queryPermissions(resultRole.getResultData());
if(resultPermission.isStatus()){
HashSet<String> permissions = new HashSet<String>();
for (RcPermission rcPermission : resultPermission.getResultData()) {
permissions.add(rcPermission.getPermissionsValue());
}
System.out.println("权限:"+permissions);
authorizationInfo.setStringPermissions(permissions);
}
}
}
return authorizationInfo;
}
}
第四步、添加shiro配置
1、shiro缓存 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE xml> <ehcache updateCheck="false" name="shiroCache"> <!-- http://ehcache.org/ehcache.xml --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" /> </ehcache> 2、在spring的core配置文件中配置shiro <description>Shiro安全配置</description> <bean id="userRealm" class="com.roncoo.adminlte.controller.realm.UserRealm" /> <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> <property name="realm" ref="userRealm" /> <property name="cacheManager" ref="shiroEhcacheManager" /> </bean> <!-- Shiro 过滤器 --> <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> <!-- Shiro的核心安全接口,这个属性是必须的 --> <property name="securityManager" ref="securityManager" /> <!-- 身份认证失败,则跳转到登录页面的配置 --> <property name="loginUrl" value="/login" /> <property name="successUrl" value="/certification" /> <property name="unauthorizedUrl" value="/error" /> <!-- Shiro连接约束配置,即过滤链的定义 --> <property name="filterChainDefinitions"> <value> /login = authc /exit = anon /admin/security/list=authcBasic,perms[admin:read] /admin/security/save=authcBasic,perms[admin:insert] /admin/security/update=authcBasic,perms[admin:update] /admin/security/delete=authcBasic,perms[admin:delete] </value> </property> </bean> <!-- 用户授权信息Cache, 采用EhCache --> <bean id="shiroEhcacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager"> <property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache-shiro.xml" /> </bean> <!-- 保证实现了Shiro内部lifecycle函数的bean执行 --> <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> <!-- AOP式方法级权限检查 --> <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"> <property name="proxyTargetClass" value="true" /> </bean> <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> <property name="securityManager" ref="securityManager" /> </bean>
第五步、shiro退出登录的实现
第一种方式
/**
* 退出登陆操作
*/
@RequestMapping(value = "/exit", method = RequestMethod.GET)
public String exit(RedirectAttributes redirectAttributes, HttpSession session) {
session.removeAttribute(Constants.Token.RONCOO);
SecurityUtils.getSubject().logout();
redirectAttributes.addFlashAttribute("msg", "您已经安全退出");
return redirect("/login");
}
第二种方式:在shiroFilter的约束配置中配置
<!-- Shiro连接约束配置,即过滤链的定义 -->
<property name="filterChainDefinitions">
<value>
/exit = logout
</value>
</property>
关注公众号
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
运用Spring Cloud搭建自我修复型分布式系统
利用Netflix所打造的组件及各类大家熟知的工具,我们完全可以顺利应对由微服务以及分布式计算所带来的技术挑战。 在过去一年当中,微服务已经成为软件架构领域一个炙手可热的新名词,而且我们也能轻松举出由其带来的诸多比较优势。然而,我们必须清醒意识到的是,一旦开始遵循微服务思路而对现有架构体系进行拆分,就意味着我们将不可避免地进入分布式系统领域。在之前的文章中我们曾经探讨过分布式计算的八大认识误区*,由此可见此类系统本身充满着风险,而且一旦犯下这八种错误中的任何一种、我们都将面对灾难性的后果。 在我看来,如果要将这些误区总结成一句观点,那就是:对于一套分布式系统来说,任何关于一致性或者可靠性的表达都毫无保障可言。我们需要假定系统当中的各种行为以及组件位置始终处于不断变化状态。由此产生的后果主要有两点:组件有时候会导致糟糕的服务质量甚至令服务直接离线,我们则只能将其统称为“故障”、而很难具体阐明到底是哪里出了问题。一旦没能得到妥善处理,此类故障将引中断与停机,这意味着系统将无法按照既定设计方案为用户提供服务项目。 有鉴于此,为了享受微服务所带来的诸多优势(包括松散耦合、自治服务、分散化治理以...
-
下一篇
Zabbix客户端日志出现(Not all processes could be identified,
场景:因为使用了netstat -p参数。 权限问题,zabbix_agentd是zabbix用户启动的,默认不能执行netstat -p等命令,导致从服务器取到的自动发现脚本为空 (Not all processes could be identified, non-owned process infowill not be shown, you would have to be root to see it all.)解决方法 :chmod +s /bin/netstat chmod +s 是什么意思 再次执行就会消失(变成正常)
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- CentOS8编译安装MySQL8.0.19
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Windows10,CentOS7,CentOS8安装Nodejs环境
- Hadoop3单机部署,实现最简伪集群
- CentOS7,CentOS8安装Elasticsearch6.8.6
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- CentOS7,8上快速安装Gitea,搭建Git服务器
- SpringBoot2全家桶,快速入门学习开发网站教程

微信收款码
支付宝收款码