spring security简单demo .

一个简单的spring security +hibernate的简单demo

环境 Spring Security 3.x

      hibernate 3

      tomcat6

1.新建一个web工程名称为spring-security

导入所需jar















以上之列出来spring及security相关jar包

2.配置web.xml

 

 

 

<?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="WebApp_ID" version="2.5">
  <display-name>spring</display-name>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <filter>
    <filter-name>struts2Filter</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/spring.xml,/WEB-INF/config/spring/spring-security.xml</param-value>
  </context-param>
  <filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <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>
  <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
<!--   <filter-mapping>
    <filter-name>struts2Filter</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping> -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>


 

spring security简单demo

分类: java 46人阅读 评论(0) 收藏 编辑 删除

一个简单的spring security +hibernate的简单demo

环境 Spring Security 3.x

      hibernate 3

      tomcat6

1.新建一个web工程名称为spring-security

导入所需jar
















以上之列出来spring及security相关jar包

2.配置web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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="WebApp_ID" version="2.5">  
  3.   <display-name>spring</display-name>  
  4.   <context-param>  
  5.     <param-name>log4jConfigLocation</param-name>  
  6.     <param-value>/WEB-INF/log4j.properties</param-value>  
  7.   </context-param>  
  8.   <listener>  
  9.     <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>  
  10.   </listener>  
  11.   <filter>  
  12.     <filter-name>struts2Filter</filter-name>  
  13.     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  14.   </filter>  
  15.   <listener>  
  16.     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
  17.   </listener>  
  18.   <context-param>  
  19.     <param-name>contextConfigLocation</param-name>  
  20.     <param-value>/WEB-INF/config/spring.xml,/WEB-INF/config/spring/spring-security.xml</param-value>  
  21.   </context-param>  
  22.   <filter>  
  23.     <filter-name>encoding</filter-name>  
  24.     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  25.     <init-param>  
  26.       <param-name>encoding</param-name>  
  27.       <param-value>UTF-8</param-value>  
  28.     </init-param>  
  29.     <init-param>  
  30.       <param-name>forceEncoding</param-name>  
  31.       <param-value>true</param-value>  
  32.     </init-param>  
  33.   </filter>  
  34.   <filter>  
  35.     <filter-name>springSecurityFilterChain</filter-name>  
  36.     <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>  
  37.   </filter>  
  38.   <filter-mapping>  
  39.     <filter-name>springSecurityFilterChain</filter-name>  
  40.     <url-pattern>/*</url-pattern>  
  41.   </filter-mapping>  
  42.   <filter-mapping>  
  43.     <filter-name>encoding</filter-name>  
  44.     <url-pattern>/*</url-pattern>  
  45.   </filter-mapping>  
  46. <!--   <filter-mapping>  
  47.     <filter-name>struts2Filter</filter-name>  
  48.     <url-pattern>*.action</url-pattern>  
  49.   </filter-mapping> -->  
  50.   <welcome-file-list>  
  51.     <welcome-file>index.jsp</welcome-file>  
  52.   </welcome-file-list>  
  53. </web-app>  
<?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="WebApp_ID" version="2.5">
  <display-name>spring</display-name>
  <context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>/WEB-INF/log4j.properties</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
  </listener>
  <filter>
    <filter-name>struts2Filter</filter-name>
    <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  </filter>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/config/spring.xml,/WEB-INF/config/spring/spring-security.xml</param-value>
  </context-param>
  <filter>
    <filter-name>encoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <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>
  <filter-mapping>
    <filter-name>encoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
<!--   <filter-mapping>
    <filter-name>struts2Filter</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping> -->
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

3.配置spring.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-3.0.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>WEB-INF/config/jdbc.properties</value><!--数据库配置存放-->
			</list>
		</property>
	</bean>
	<!-- 以下配置数据源, 使用连接池 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect"> org.hibernate.dialect.OracleDialect
				</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
		<property name="mappingLocations">
			<list>
				<value>classpath:/com/common/dm/*.hbm.xml</value>
			</list>
		</property>
	</bean>
	<bean id="usersDao" class="com.common.dao.UsersDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

</beans>

3.spring- security.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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-3.1.xsd">

	<global-method-security pre-post-annotations="enabled" />
	<http auto-config="true" access-denied-page="/403.jsp"><!--  自定义无权限页面 -->
		<!-- 自定义登录页面,若不定义则spring自动生成 -->
		<form-login login-page="/login.jsp" />
		<intercept-url pattern="/login.jsp*" access="IS_AUTHENTICATED_ANONYMOUSLY" /><!--不进行拦截  -->
		<intercept-url pattern="/index.jsp*" access="ROLE_ADMIN,ROLE_VISIT" />
		<intercept-url pattern="/admin.jsp*" access="ROLE_ADMIN" />
	</http>
	<authentication-manager>
		<authentication-provider user-service-ref="myAuthenticationProvider">

		</authentication-provider>
		<!-- <authentication-provider> <user-service> <user name="WJL" password="WJL" 
			authorities="ROLE_ADMIN"/> </user-service> </authentication-provider> -->
	</authentication-manager>
	<beans:bean id="myAuthenticationProvider"
		class="com.common.security.MyhAuthenticationProvider">
		<beans:property name="usersDao" ref="usersDao"></beans:property>
	</beans:bean>
</beans:beans>

在MyhauthenticationProvider中进行与数据库的交互;

代码如下

package com.common.security;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.GrantedAuthorityImpl;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import com.common.dao.UsersDao;
import com.common.dm.Users;

/**
 * <p>
 * mailto: reven_wjl@163.com
 * </p>
 * 
 * @author reven
 * @date 2012-9-23
 * @version 1.0.1
 */
public class MyhAuthenticationProvider implements UserDetailsService {

	private UsersDao usersDao;

	public UsersDao getUsersDao() {
		return usersDao;
	}

	public void setUsersDao(UsersDao usersDao) {
		this.usersDao = usersDao;
	}

	public UserDetails loadUserByUsername(String userName)
			throws UsernameNotFoundException {
		// TODO Auto-generated method stub
		Collection<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();

		Users user = usersDao.getUserByUserName(userName);
		if(user != null){
			
			String password = user.getPassword();
			String limit = user.getLimits();
			if (limit.equals("1")) {
				GrantedAuthorityImpl auth = new GrantedAuthorityImpl("ROLE_ADMIN");
				auths.add(auth);
			}
			if (limit.equals("0")) {
				GrantedAuthorityImpl auth = new GrantedAuthorityImpl("ROLE_VISIT");
				auths.add(auth);
			}
			User userspring = new User(userName, password, true, true, true, true,
					auths);
			return userspring;
		}else{
			return null;
		}
		
		

		
	}
}


与数据库交互的方法就不列举了,很简单


login.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=UTF-8">
<title>用户登录</title>
<script>
</script>
</head>
<body>
<h3>用户登录</h3>
<form action='/spring-security/j_spring_security_check' method='POST'>
用户名:<input type="text" name="j_username"/>
密码:<input type="password" name="j_password"/>
<input type="submit">
<label style="color: red">${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}</label>//错误信息
</form>
</body>
</html>


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
04-26
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值