温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

跟我学习Spring Security--在线宠物商店开发(二)

发布时间:2020-05-31 05:14:44 来源:网络 阅读:836 作者:zangyanan2016 栏目:数据库

    我们首先来一个简单Spring Security登录,首先需要搭建环境,这里我们用Spring+SpringMVC+Spring Security,数据库用Hibernate4+Oracle,关于jar包,Spring以及SpringMVC我用的是3.2版本的。

    在web.xml中我们主要是配置Spring、SpringMVC以及Spring Security的集成。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="DogStoreApp" version="2.5">
  <display-name>Dog Store</display-name>
   <!-- 集成spring的通用配置 -->
   <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
    
   </param-value>
   </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- SpringMVC前端控制器 -->
 <servlet>
 <servlet-name>dogstore</servlet-name>
 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>  
 </servlet>
 <servlet-mapping>
    <servlet-name>dogstore</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <!-- springSecurity核心过滤器配置 -->
  <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

    现在我们需要配置Spring Security的组件,在配置Spring Security的权限控制文件之前,我们来拓展一下Spring Security权限控制方法:

1、不用数据库,全部数据写在配置文件,这个也是官方文档里面的demo;

2、使用数据库,根据spring security默认实现代码设计数据库,也就是说数据库已经固定了,这种方法不灵活,而且那个数据库设计得很简陋,实用性差;

3、spring security和Acegi不同,它不能修改默认filter了,但支持插入filter,所以根据这个,我们可以插入自己的filter来灵活使用;

4、暴力手段,修改源码,前面说的修改默认filter只是修改配置文件以替换filter而已,这种是直接改了里面的源码,但是这种不符合OO设计原则,而且不实际,不可用。

现在配置dogstore-security.xml这个文件,关于命名空间配置,官方提供了两种配置方案

<beans xmlns="http://www.springframework.org/schema/beans"  
  xmlns:security="http://www.springframework.org/schema/security"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
          http://www.springframework.org/schema/security  
          http://www.springframework.org/schema/security/spring-security.xsd">  
    ...  
</beans>

第二种、命名空间用security开头,在配置中不需要security前缀,但是bean的配置需要用<beans:bean>配置

<beans:beans xmlns="http://www.springframework.org/schema/security"  
  xmlns:beans="http://www.springframework.org/schema/beans"  
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  xsi:schemaLocation="http://www.springframework.org/schema/beans  
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
           http://www.springframework.org/schema/security  
           http://www.springframework.org/schema/security/spring-security.xsd">  
    ...  
</beans:beans>

首先我们先按照上面第一种权限控制来做个简单的demo:dogstore-security.xml,注意自己引用的jar与命名空间版本要一致

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans 
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/security 
		http://www.springframework.org/schema/security/spring-security-3.1.xsd">
	<http auto-config="true">
		<intercept-url pattern="/*" access="ROLE_USER"/>
	</http>
	<authentication-manager alias="authenticationManager">
		<authentication-provider>
			<user-service>
				<user authorities="ROLE_USER" name="guest" password="guest"/>
			</user-service>
		</authentication-provider>
	</authentication-manager>	
</beans:beans>

      第一个http标签其实主要是配置拦截url用的,里边大概配置了如果你要访问某个路径,需要哪个连接权限,而http标签下边的authentication-manger标签下的标签则配置了那些用户都拥有哪些权限,目前我们先暂时按这个步骤去做,后面详细介绍。

   现在我们来完善Spring、SpringMVC所需的配置文件,上面web.xml中我们已经预留了contextConfigLocation来引入配置文件,首先创建dogstore-base.xml空文件,这是Spring配置文件,如果后面需要什么,我们再添加

<?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:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
	">
 
</beans>

SpringMVC的配置文件,先配置视图解析,SpringMVC配置会自动查找配置文件,Servlet的名字是(<servlet-name>)是dogstore,约定胜于配置将会在WEB-INF目录下寻找名为dogstore-servelt.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:jdbc="http://www.springframework.org/schema/jdbc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/jdbc  http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
	">	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
	    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
	   <property name="prefix" value=""/>
	   <property name="suffix" value=".jsp"/>
	</bean>
</beans>

目前Spring、SpringMVC以及Spring Security配置文件基本用法已经配置,引入web.xml,SpringMVC自己引入配置文件。

<context-param>  
  <param-name>contextConfigLocation</param-name>  
  <param-value>  
    /WEB-INF/dogstore-security.xml  
    /WEB-INF/dogstore-base.xml  
  </param-value>  
</context-param>

将上面配置的加入tomcat,启动,在没有自定义登录页面之前,SpringSecurity会自动生成登录页面,如下图,我们在web.xml下添加首页来验证拦截是否有效

 <welcome-file-list>
  <welcome-file>main.jsp</welcome-file>
  </welcome-file-list>

在WebContent下添加main.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
我已经登录进来了!
</body>
</html>


 跟我学习Spring Security--在线宠物商店开发(二)

输入错误的账号或者错误密码

 跟我学习Spring Security--在线宠物商店开发(二)

输入上面配置的guest/guest,登录成功。




向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI