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

我们首先来一个简单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="/WEB-INF/views/"/>
	   <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>

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

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

时间: 2024-08-11 21:13:36

跟我学习Spring Security--在线宠物商店开发(二)的相关文章

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

本应用遵循标准的三层结构,包括web层.服务层和数据访问层,如下图所示: web层封装了MVC的代码和功能.在示例代码中,我们使用了Spring MVC框架,但是我们可以一样容易的使用Spring Web Flow,Struts甚至是一个对Spring友好的web stack如Apache Wicket. 在一个典型使用Spring Security的web应用中,大量配置和参数代码位于web层.所以,如果你没有web应用开发,尤其是Spring MVC的经验,在我们进入更复杂的话题前,你最好仔

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

前面我们用了简单的三步就完成了一个Spring- Secutiry的搭建,主要是通过添加auto-config属性和http元素实现的,但是在实际工作中要比这个复杂的太多,现在我们按部就班的来完善我们的权限管理,遇到理论问题,可以去查看专家博客:http://lengyun3566.iteye.com/blog关于Spring Secutiry3翻译. 现在我们为我们JBCP Pets在线商店完善一下,我们先配置自己默认的登录界面,有兴趣的可以从网上找一个,这里我不用书上的登录界面,我从网上找的

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

前面几节都是将用户以及权限都配置在配置文件,在企业中,这种方法是不可取的,因此,我们今天来将用户信息以及权限信息移到数据库. 为了从数据库中获取用户权限信息,我们所需要的仅仅是修改配置文件中的authentication-provider部分,将文件中的user-service替换为jdbc-user-service,替换内容如下所示: <authentication-manager alias="authenticationManager"> <authentica

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

我们知道将信息存在cookie中会存在一定的被拦截并被重用使用的风险,如图所示: 有一种让remember me功能更安全的方式就是将用户的IP地址绑定到cookie的内容上.让我们通过一个例子来描述怎样构建RememberMeServices的实现类来完成这个功能. 基本的实现方式是扩展o.s.s.web.authentication.rememberme.TokenBasedRememberMeServices基类,以添加请求者的IP地址到cookie本身和其他的MD5哈希元素中. 扩展这个

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

现在我们来用Spring Security检测一下登录功能,添加在views下添加一个main.jsp <%@ page language="java" contentType="text/html; charset=UTF-8"     pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 

Spring Security 自定义登录认证(二)

一.前言 本篇文章将讲述Spring Security自定义登录认证校验用户名.密码,自定义密码加密方式,以及在前后端分离的情况下认证失败或成功处理返回json格式数据 温馨小提示:Spring Security中有默认的密码加密方式以及登录用户认证校验,但小编这里选择自定义是为了方便以后业务扩展,比如系统默认带一个超级管理员,当认证时识别到是超级管理员账号登录访问时给它赋予最高权限,可以访问系统所有api接口,或在登录认证成功后存入token以便用户访问系统其它接口时通过token认证用户权限

spring security源码分析之二---web包分析

Spring 是一个非常流行和成功的 Java 应用开发框架.Spring Security 基于 Spring 框架,提供了一套 Web 应用安全性的完整解决方案.一般来说,Web 应用的安全性包括用户认证(Authentication)和用户授权(Authorization)两个部分.用户认证指的是验证某个用户是否为系统中的合法主体,也就是说用户能否访问该系统.用户认证一般要求用户提供用户名和密码.系统通过校验用户名和密码来完成认证过程.用户授权指的是验证某个用户是否有权限执行某个操作.在一

Spring Security和Apache Shiro开发企业级权限管理系统

原理回顾什么是权限管理?权限管理是系统的安全范畴,要求必须是合法的用户才可以访问系统(用户认证),且必须具有该 资源的访问权限才可以访问该 资源(授权).认证:对用户合法身份的校验,要求必须是合法的用户才可以访问系统.授权:访问控制,必须具有该 资源的访问权限才可以访问该 资源.权限模型:标准权限数据模型包括 :用户.角色.权限(包括资源和权限).用户角色关系.角色权限关系.权限分配:通过UI界面方便给用户分配权限,对上边权限模型进行增.删.改.查操作.权限控制:基于角色的权限控制:根据角色判断

[转]Spring Security学习总结二

原文链接: http://www.blogjava.net/redhatlinux/archive/2008/08/20/223148.html http://www.blogjava.net/redhatlinux/archive/2008/09/01/226010.html [总结-含源码]Spring Security学习总结二 Posted on 2008-09-01 10:08 tangtb 阅读(9518) 评论(12)  编辑  收藏 所属分类: Spring .Spring Se