`

SpringMVC3.2.x + Hibernate4.2.x + ecache + Spring Security 3.0.5

    博客分类:
  • SSH
 
阅读更多
这只是部分代码,一些代码可以参考:http://panyongzheng.iteye.com/blog/1871418


Spring Security3.1 最新配置实例http://blog.csdn.net/k10509806/article/details/6369131, 它自带的附件也上传。
Spring Security3十五日研究 http://www.blogjava.net/SpartaYew/archive/2013/09/23/350630.html,这里描述了几种使用方式。

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>spring_security_demo</groupId>
    <artifactId>spring_security_demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>3.2.2.RELEASE</spring.version>
        <spring.jdbc.version>3.2.2.RELEASE</spring.jdbc.version>
        <aspectj.version>1.6.10</aspectj.version>
        <jetty.version>6.1.26</jetty.version>
        <tomcat.version>2.0</tomcat.version>
        <junit.version>4.11</junit.version>
        <mysql.version>5.1.25</mysql.version>
        <jtds.version>1.2.4</jtds.version>
        <hibernate.version>4.2.1.Final</hibernate.version>
        <hibernateCommAnn.version>3.2.0.Final</hibernateCommAnn.version>
        <hibernateAnn.version>3.5.6-Final</hibernateAnn.version>
        <cglib.version>2.2.2</cglib.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>net.sourceforge.jtds</groupId>
            <artifactId>jtds</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>sqljdbc</groupId>
            <artifactId>sqljdbc</artifactId>
            <version>4.0</version>
        </dependency>

        <!-- JBOSS Cache & Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>4.2.1.Final</version>
        </dependency>
        <dependency>
            <groupId>hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <version>1.8.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-testing</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache-core</artifactId>
            <version>2.4.3</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>

        <!-- Spring -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.jdbc.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.jdbc.version}</version>
        </dependency>

        <!-- Spring Security -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-core</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>3.0.5.RELEASE</version>
        </dependency>


        <!-- Hibernate -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>${hibernate.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-commons-annotations</artifactId>
            <version>${hibernateCommAnn.version}</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>${hibernateAnn.version}</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>${cglib.version}</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.8.3</version>
        </dependency>

        <!-- Log4j -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.1</version>
        </dependency>
        <dependency>
            <groupId>org.lazyluke</groupId>
            <artifactId>log4jdbc-remix</artifactId>
            <version>0.2.7</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>spring_security_demo</finalName>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.mortbay.jetty</groupId>
                    <artifactId>maven-jetty-plugin</artifactId>
                    <version>${jetty.version}</version>
                </plugin>
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat6-maven-plugin</artifactId>
                    <version>${tomcat.version}</version>
                </plugin>
                <plugin>
                    <!-- 注解支持,jdk5才具有的新特性,我们需要设置compile插件,具体可以参考Setting the -source and
                        -target of the Java Compiler,根据说明,我们继续向pom文件中加入 -->
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>5</source>
                        <target>5</target>
                    </configuration>
                </plugin>
                <plugin>
                    <!-- 我们想将所有的依赖库都打包,直接交给用户,这样用户不需要在做其他设置了,这里需要使用Assembly插件了,其说明参考Pre-defined
                        Descriptor Files,这个参考文件也说明了有四种默认定义的打包方式,我们选择jar-with-dependencies,继续添加pom文件 -->
                    <artifactId>maven-assembly-plugin</artifactId>
                    <configuration>
                        <descriptorRefs>
                            <descriptorRef>jar-with-dependencies</descriptorRef>
                        </descriptorRefs>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <id>copy</id>
                            <phase>install</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>$/lib</outputDirectory>
                                <!-- 拷贝所以依赖存放位置 -->
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>

        </pluginManagement>
    </build>

</project>


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
		  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <display-name>spring_security_demo</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml,classpath:spring-security.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springmvc_hibernate_demo</servlet-name>
        <servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc_hibernate_demo</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
    <!-- spring security -->
    <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>
        <filter-name>openSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

</web-app>



applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"
       default-lazy-init="true">
       <!--default-autowire="byName"-->
    <context:component-scan base-package="com">
        <context:exclude-filter expression="org.springframework.stereotype.Controller"
                                type="annotation" />
    </context:component-scan>
    <bean
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!--<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver"/>
        <property name="url" value="jdbc:sqlserver://192.168.0.196:1433;DatabaseName=dev_cpm_pri"/>-->
        <property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
        <property name="url" value="jdbc:jtds:sqlserver://192.168.0.168:1433/dev_cpm_pri"/>

        <property name="username" value="sa"/>
        <property name="password" value="sa"/>
    </bean>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:packagesToScan="com.pandy.model">
        <property name="dataSource">
            <ref bean="dataSource" />
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">
                    ${hibernate.dialect}
                </prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</prop>
                <prop key="hibernate.cache.use_structured_entries">true</prop>
                <prop key="hibernate.generate_statistics">true</prop>
            </props>
        </property>
    </bean>
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref local="sessionFactory" />
        </property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>


servlet.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:p="http://www.springframework.org/schema/p"
       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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

    <context:component-scan base-package="com">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" />
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
    </context:component-scan>

    <mvc:annotation-driven/>

    <mvc:resources location="/resources/" mapping="/resources/**"/>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

</beans>


HelloController.java
package com.pandy.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping(method = RequestMethod.GET)
    public ModelAndView printHello() {
        return new ModelAndView("hello", "message", "Hello Spring Security !");
    }
}


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>
</head>
<body>
<form id="loginForm" action="./j_spring_security_check" method="post">
    <table class="inputView">
        <tr>
            <td>
                用户名:
            </td>
            <td>
                <input type='text' name='j_username'/>
            </td>
        </tr>
        <tr>
            <td>
                密码:
            </td>
            <td>
                <input type='password' name='j_password'/>
            </td>
        </tr>
        <tr>
            <td colspan='2'>
                <input value="登录" type="submit" />
            </td>
        </tr>
    </table>
</form>
</body>
</html>


1. 直接硬编码账户和密码的配置
spring-security.xml
<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.0.3.xsd">

    <http auto-config="true">
        <intercept-url pattern="/hello*" access="ROLE_USER" />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="admin" password="123456" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>
分享到:
评论

相关推荐

    springMVC+srping3.2+hibernate4.2+bonecp登陆示例

    最新的springMVC+srping3.2+hibernate4.2的一个登陆示例,连接池使用bonecp,中间加了一个检测页面URL合法性的filter,里面可以增加session检测等,适用于此框架的初学者(因只是通过一个登陆页面将框架构建出来) 注意:这...

    springmvc, spring5.x , hibernate5.x 整合

    整合Spring MVC、Spring 5.x和Hibernate 5.x,我们可以构建一个强大的后端系统。首先,我们需要在Spring配置中声明Hibernate的DataSource和SessionFactory,然后通过Spring的JdbcTemplate或Hibernate的Session接口...

    spring5.x+springmvc5.x+mybatis3.5x+tomcat9+jdk8+maven 项目框架搭建 1.1版本

    本项目框架搭建基于"spring5.x+springmvc5.x+mybatis3.5x+tomcat9+jdk8+maven",这是一个经典且广泛使用的Java开发配置。下面将详细阐述这些组件的功能、相互关系以及如何进行集成。 1. **Spring 5.x**: Spring是...

    Hibernate4.2+Spring3.2+SpringMVC+shiro1.2.2+Maven

    Hibernate4.2+Spring3.2+SpringMVC+shiro1.2.2+Maven整合和简单的登陆,导入工程后见description,内容简单基础,想做成脚手架供自己以后使用,望高手勿喷有什么宝贵意见望留言,谢谢

    springmvc+spring+hibernate

    Spring MVC、Spring 和 Hibernate 是Java Web开发中的三大主流框架,它们各司其职,共同构建了一个强大而灵活的后端架构。Spring MVC 负责处理HTTP请求并将其路由到相应的控制器,Spring 提供了依赖注入(DI)和面向...

    spring4.x+springMVC4.x+mybatis3.x+ehcache2.6.x整合

    以前都是用SSH,第一次使用spring+springMVC+mybatis自己做的测试练习,主要是配置注解的使用,加了稍微详细的注释说明。另外在测试ehcache的时候也继承进去了,紧做练习参考. 环境:tomcat使用7.x,JDK1.7开发工具...

    Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA

    在现代Java Web开发中,"Maven整合Spring+SpringMVC+Hibernate+SpringDataJPA"是一个常见的架构组合,被广泛应用于构建企业级应用程序。这个组合通常被称为"SSM",其中"M"代表Maven,"S"代表Spring,包括Spring核心...

    maven3.05+springmvc3.2+spring3.2+hibernate3.6重写传智播客OA源代码(包含数据库)

    maven3.05+springmvc3.2+spring3.2+hibernate3.6重写传智播客OA源代码。菜单部分采用ztree.界面有一部分使用bootstrap3.11。听了汤阳光大神的视频收获很大,也学习springmvc,期间有一个想法用springmvc来改写汤大神...

    SpringMVC+Spring3.2+Hibernate4整合.zip

    在SpringMVC+Spring3.2+Hibernate4的整合中,Spring管理着所有bean,包括SpringMVC的控制器、服务层接口及实现、以及Hibernate的SessionFactory和DAO。通过Spring的TransactionManagement,可以方便地进行事务控制,...

    基于SpringMVC+Spring3+Mybatis框架的OA项目源码.zip

    基于SpringMVC+Spring3+Mybatis框架的OA项目源码.zip 基于SpringMVC+Spring3+Mybatis框架的OA项目源码.zip 基于SpringMVC+Spring3+Mybatis框架的OA项目源码.zip 基于SpringMVC+Spring3+Mybatis框架的OA项目源码.zip ...

    springMVC3.2+Hibernate4的基本配置

    在本文中,我们将深入探讨如何配置一个基于SpringMVC 3.2和Hibernate 4的基础项目。这个项目配置涵盖了核心的框架组件,如数据库连接池、事务管理、缓存策略以及拦截器,这些是构建现代Java Web应用的关键要素。我们...

    SpringMVC+Spring3+Hibernate4

    SpringMVC、Spring3和Hibernate4是Java Web开发中三个非常重要的框架,它们共同构建了一个强大的后端架构。SpringMVC作为Spring框架的一部分,主要负责处理HTTP请求和响应,Spring3则提供了依赖注入(DI)和面向切面...

    springmvc+spring4.x+hibernate4.x整合实现对学生信息的增删改查

    在本项目中,我们主要探讨的是如何将Spring MVC、Spring 4.x 和 Hibernate 4.x 框架整合,以实现对学生信息的CRUD(创建、读取、更新和删除)操作。数据库方面,选用的是Microsoft的SQL Server 2005。下面,我们将...

    idea工具创建的Spring+SpringMVC+Hibernate+maven项目

    标题中的"idea工具创建的Spring+SpringMVC+Hibernate+maven项目"指的是使用IntelliJ IDEA这个集成开发环境(IDE)构建的一个Java Web项目,该项目整合了四个关键的技术框架:Spring、SpringMVC、Hibernate以及Maven...

    SpringMVC+Spring3.2+Hibernate4整合

    **SpringMVC+Spring3.2+Hibernate4整合详解** 在现代Java开发中,Spring框架以其模块化、可扩展性和企业级应用支持而备受青睐。本篇将详细讲解如何将SpringMVC、Spring3.2和Hibernate4进行整合,构建一个高效、灵活...

    springMVC3.2+Hibernate4+freemarker

    springMVC3.2+Hibernate4+freemarker 代码框架采用springMVC3.2.4+hibernate4.2.8+freemarker2.3.16 功能方面只是一个简单的注册登录,前台使用freemarker渲染,使用了freemarker自定义标签。

    ssh(spring+strut1.x+hibernate)整合完整教程 附带包

    SSH整合是Java Web开发中的一个经典组合,由Spring、Struts1.x和Hibernate三个框架构成。这个整合提供了全面的企业级应用解决方案,涵盖了控制层、业务层和服务数据持久化层的管理。下面将详细讲解SSH整合的主要知识...

    SpringMVC+Spring+Hibernate+Oracle 实现图书管理(CRUD)

    在这个案例中,"SpringMVC+Spring+Hibernate+Oracle 实现图书管理(CRUD)"是一个使用了四大主流Java技术栈的项目,分别是SpringMVC作为前端控制器、Spring作为应用层框架、Hibernate作为持久层框架,以及Oracle作为...

    spring mvc + spring + hibernate 全注解整合开发视频教程 11

    在本教程中,我们将深入探讨如何使用Spring MVC、Spring和Hibernate三大框架进行全注解的整合开发。这个视频教程系列的第11部分,重点可能是建立在前几部分的基础之上,进一步深化对这三个核心技术的理解和实践。 ...

Global site tag (gtag.js) - Google Analytics