authc过滤器 shiro_shiro 安全框架(2)

Shiro-与Spring整合实现登录认证之配置文件编写

Shiro-与Spring整合实现登录认证有哪些操作步骤?

实现步骤

A.新建web项目

B.导入shiro相关jar包(shiro-all 以及shiro-spring)

C.在web.xml中添加DelegatingFilterProxy配置

D.编写spring-shiro.xml E.编写mapper接口及mapper.xml

F.编写service接口及实现类 G.编写controller

H.编写Realm

DelegatingFilterProxy的作用

DelegatingFilterProxy类存在与spring-web包中,其作用就是一个filter的代理,用这个类的好处是可以通过spring容器来管理filter的生命周期,还有就是,可以通过spring注入的形式,来代理一个filter执行,如shiro,下面会说到;有上图我们可以看到,DelegatingFilterProxy类继承GenericFilterBean,间接实现了Filter这个接口,故而该类属于一个过滤器。那么就会有实现Filter中init、doFilter、destroy三个方法。

(3) 需要将Shiro中的哪些对象交给spring容器管理?

凭证管理器、自定义Realm、注册SecurityManager

Shiro-与Spring整合实现登录认证之代码编写

(1) 通过哪个对象可以获取表单认证异常信息?

FormAuthenticationFilter

 /查看具体异常信息和获取异常信息
 Object ex = request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);

(2) 使用FormAuthenticationFilter进行验证的时候需要注意什么?

表单属性必须为username和passwrod

Shiro--实现注册并密码加密

 index.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  <script type="text/javascript" src="${ctx}/js/jquery-1.7.2.min.js"></script>
  <script type="text/javascript">
     $(function(){
		$("#btn1").click(function() {
			$.ajax({
				url:"${ctx}/user/register.do",
				data:$("#myform").serialize(),
				success:function(data){
					alert(data);
				}
			})
		})
	})
  
   
  </script>
<html>
  <head>
  	<title>首页</title>
  </head>
  
  <body>
    <form action="${ctx}/user/login.do" method="POST" id="myform">
    	用户名:<input type="text" name="username"><br> 
    	密码:<input type="password" name="password"><br>
    	<input type="submit" value="提交">
    	<input type="button" value="注册" id="btn1">
    </form>
  </body>
</html>

spring-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" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.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>
    <!-- 注册自定义Realm -->
    <bean id="customRealm" class="com.bjsxt.realms.CustomRealm">
    	<property name="credentialsMatcher" ref="credentialsMatcher"></property>
    </bean>
    <!-- 注册SecurityManager -->
    <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    	<property name="realm" ref="customRealm"></property>
    </bean>
    <!-- 注册ShiroFilterFactoryBean 注意:id名称必须与web.xml中过滤器名称对应 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
    	<property name="securityManager" ref="securityManager"></property>
    	<property name="loginUrl" value="/user/login.do"></property>
    	<property name="successUrl" value="/jsp/users.jsp"></property>
    	<property name="unauthorizedUrl" value="/jsp/refuse.jsp"></property>
    	<!-- 设置过滤器链属性 -->
    	<property name="filterChainDefinitions">
    		<value>
    			/user/login.do=authc
    			/**=anon
    		</value>
    	</property>
    </bean>
</beans>
 package com.bjsxt.rbac.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.apache.tomcat.jni.User;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bjsxt.rbac.pojo.Users;
import com.bjsxt.rbac.service.IUserService;

@Controller
@Scope("prototype")
@RequestMapping("/user")
public class UserController {
	@Resource
	private IUserService userService;
	/**
	 * 设定登录失败跳转的资源以及获取异常信息
	 */
	@RequestMapping("/login.do")
	public String login(HttpServletRequest request,Model model){
		//查看具体异常信息和获取异常信息
		Object ex = request.getAttribute(FormAuthenticationFilter.DEFAULT_ERROR_KEY_ATTRIBUTE_NAME);
		if(UnknownAccountException.class.getName().equals(ex)){
			model.addAttribute("msg", "账户不正确");
		}else if(IncorrectCredentialsException.class.getName().equals(ex)){
			model.addAttribute("msg", "凭证不正确");
		}else{
			System.out.println(ex);
			model.addAttribute("msg", "未知验证异常");
		}
		return "/jsp/exception.jsp";
	}	
	@RequestMapping(value="/register.do",produces="text/html;charset=utf-8")
	@ResponseBody
	public String register(String username, String password){
		Users users = new Users();
		users.setUsername(username);
		users.setPassword(password);
		userService.insertUesr(users);
		return "注册成功";
		
	}
}

Shiro-实现菜单授权

(1) 实现授权功能Realm需要继承哪个类?

AuthorizingRealm

(2) 在哪个方法中完成授权的代码?

doGetAuthorizationInfo

Shiro--SessionManager的使用

(1) SessionManager是什么,具有什么作用?

SessionManager会话管理器管理着应用中所有Subject的会话的创建、 维护、删除、失效、验证等工作。

(2) Shiro默认提供了哪几种SessionManager实现,以及他们的应用场景是什么?

Shiro提供了三个默认实现: DefaultSessionManager:DefaultSecurityManager使用的默认实现,用于 JavaSE环境; ServletContainerSessionManager:用于Web环境,其直接使用Servlet容器 的会话; DefaultWebSessionManager:用于Web环境的实现,可以替代 ServletContainerSessionManager,自己维护着会话,直接废弃了Servlet 容器的会话管理。

Shiro-RememberMe的使用

Shiro内置过滤器

f0e4f73fff14a49af38c379adb09589b.png

这些过滤器分为两组,一组是认证过滤器,一组是授权过滤器。 其中anon,authcBasic,auchc,user是第一组, perms,roles,ssl,rest,port是第二组

rest:例子/admins/user/**=rest[user],根据请求的方法,相当于 /admins/user/**=perms[user:method] ,其中method为post,get,delete等。 port:例子/admins/user/**=port[8081],当请求的url的端口不是8081是跳转到 schemal://serverName:8081?queryString,其中schmal是协议http或https等, serverName是你访问的host,8081是url配置里port的端口,queryString 是你访问的url里的?后面的参数。 perms:例子/admins/user/**=perms[user:add:*],perms参数可以写多个,多个时必 须加上引号,并且参数之间用逗号分割,例如 /admins/user/**=perms["user:add:*,user:modify:*"],当有多个参数时必须每个 参数都通过才通过,想当于 isPermitedAll()方法。 roles:例子/admins/user/**=roles[admin],参数可以写多个,多个时必须加上引号, 并且参数之间用逗号分割,当有多个参数时,例如 /admins/user/**=roles["admin,guest"],每个参数通过才算通过,相当于 hasAllRoles()方法。 anon:例子/admins/**=anon 没有参数,表示可以匿名使用。 authc:例如/admins/user/**=authc表示需要认证才能使用,没有参数 authcBasic:例如/admins/user/**=authcBasic没有参数表示httpBasic认证 ssl:例子/admins/user/**=ssl没有参数,表示安全的url请求,协议为https

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值