spring mvc ,spring security配置 spring-servlet.xml和spring-security.xml设置

spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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">

    <mvc:annotation-driven></mvc:annotation-driven>
    <mvc:resources mapping="/static/**" location="/statics/"></mvc:resources>
    <mvc:resources mapping="/resources/**" location="/resources/"></mvc:resources>
    <!-- 配置视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/resources/jsp/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    
    <!-- 从请求和响应 读取/编写字符串 -->
    <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
        <property name="supportedMediaTypes">
            <list>
                <value>text/plain;charset=UTF-8</value>
            </list>
        </property>
    </bean>
</beans>

spring-security.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">

    <security:http pattern="/statics/**" security="none"/>
    <security:http  auto-config="true" use-expressions="true">
        <security:intercept-url pattern="/login.do" access="isAnonymous()"/>
        <security:intercept-url pattern="/register.do" access="isAnonymous()"/>
        <security:intercept-url pattern="/registerusers.do" access="isAnonymous()"/>
        <security:intercept-url pattern="/useradd.do" access="isAnonymous()"/>
        <security:intercept-url pattern="/admins/**" access="hasRole(‘ROLE_ADMIN‘)"/>
        <security:intercept-url pattern="/**" access="hasRole(‘ROLE_USER‘)"/>
        <security:csrf disabled="false" token-repository-ref="cookieCsrfTokenRepository" />
        <security:form-login login-page="/login.do" login-processing-url="/login"  username-parameter="username" password-parameter="password" authentication-failure-url="/login.do?error=true" />
        <security:logout invalidate-session="true" logout-url="/logout" logout-success-url="/login.do"/>
        <security:http-basic />
        <security:remember-me data-source-ref="dataSource" key="youkey" remember-me-parameter="remember-me"/>
        <security:session-management>
            <security:concurrency-control  />
        </security:session-management>
    </security:http>
    <security:authentication-manager>
        <!--静态添加的用户登录信息-->
        <!--<security:authentication-provider>
            <security:user-service>
                <security:user name="admin" password="admin123" authorities="ROLE_USER,ROLE_ADMIN"/>
                <security:user name="user" password="user123" authorities="ROLE_USER"/>
            </security:user-service>
        </security:authentication-provider>-->
        <security:authentication-provider>
            <security:password-encoder ref="bCryptPasswordEncoder"/>
            <security:jdbc-user-service id="userDetailsService" data-source-ref="dataSource"
                                        users-by-username-query="SELECT username,password,enabled FROM users WHERE username=?"
                                        authorities-by-username-query="SELECT u.username as username,r.rolename as authority FROM users u join userrole ur on u.userid=ur.userid join roles r on r.roleid=ur.roleid WHERE u.username=?"
            />
        </security:authentication-provider>
    </security:authentication-manager>

    <bean id="bCryptPasswordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
    <bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <property name="hideUserNotFoundExceptions" value="false"/>
        <property name="userDetailsService" ref="userDetailsService"/>
        <property name="passwordEncoder" ref="bCryptPasswordEncoder"/>

    </bean>
    <bean id="cookieCsrfTokenRepository" class="org.springframework.security.web.csrf.CookieCsrfTokenRepository">
        <property name="cookieHttpOnly" value="false"/>
    </bean>

  
</beans>
时间: 2024-08-11 03:31:41

spring mvc ,spring security配置 spring-servlet.xml和spring-security.xml设置的相关文章

Spring MVC全注解配置 - 无web.xml

Serlvet 3以后,我们可以使用注解来配置Servlet,对于像Spring这类的框架来说是一个很好的适应.Spring也对此特性加入了很多的新功能.本文就将简单的对之前的xml配置转换为java代码的配置.代码配置让程序员们觉得更加具有流程化,不像配置很多代码程序员们都不愿意look into. 接下来,进行替换我们之前的web.xml和spring-mvc.xml的配置.也就是在你的web工程里面看不到这两个配置文件了.(可能有的童鞋会说,这样配置可能对以后的修改不方便,无法达到只修改配

Spring4.X + spring MVC + Mybatis3 零配置应用开发框架搭建详解(1) - 基本介绍

Spring4.X + spring MVC + Mybatis3 零配置应用开发框架搭建详解(1) - 基本介绍 spring集成 mybatis Spring4.x零配置框架搭建 两年前一直在做后台的纯Java开发,很少涉及web开发这块,最近换了个纯的互联网公司,需要做Web后台管理系统,之前都是用xml配置的项目,接触了公司Spring4.x的零配置项目,觉得非常有感觉,不仅仅配置简单,而且条理清晰,所以,这里把学习的内容记录下来,一来加深对这块技术的印象,另外准备做个简单的教程,如果给

零基础搭建 spring mvc 4 项目(本文基于 Servlet 3.0)

作者各必备工具的版本如下: Tomcat:apache-tomcat-7.0.63 (下载链接) Java EE - Eclipse:Luna Service Release 1 v4.4.1 (下载链接) Spring:4.2.0.RELEASE (无须下载) JDK:1.7.0_67 (下载链接) 步骤 1 使用 Java EE - Eclipse 新建一 Dynamic Web Project. 步骤 2 输入项目名 bdp.Target Runtime 选 Apache Tomcat 7

Spring MVC原理及配置详解

Spring MVC原理及配置 1.Spring MVC概述: Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单.这些控制器一般不直接处理请求,而是将其委托给Spring上下文中的其他bean,通过Spring的依赖注入功能,这些bean被注入到控制器中. Spring MVC主要由DispatcherServlet.处理器映射.处理器(控制器).视图解析器.视图组成.他的两个核心是

Spring MVC原理及配置

Spring MVC原理及配置 1. Spring MVC概述 Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单.这些控制器一般不直接处理请求,而是将其委托给Spring上下文中的其他bean,通过Spring的依赖注入功能,这些bean被注入到控制器中. Spring MVC主要由DispatcherServlet.处理器映射.处理器(控制器).视图解析器.视图组成. 2. Spr

spring MVC、mybatis配置读写分离

spring MVC.mybatis配置读写分离 1.环境: 3台数据库机器,一个master,二台slave,分别为slave1,slave2 2.要实现的目标: ①使数据写入到master ②读数据时,从slave1和slave2中读取 以此来实现数据库的读写分离 3.原理: 使用MySQL驱动自带的replicationDriver来实现,replicationDriver简单来说就是存在两个Connection,一个masterConnection,一个slaveConnection:

spring 4 + jpa(hibernate 3/4) + spring mvc 多数据源配置

先从persistence.xml开始: <?xml version=”1.0″ encoding=”UTF-8″?><persistence version=”2.1″ xmlns=”http://java.sun.com/xml/ns/persistence” xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xsi:schemaLocation=”http://java.sun.com/xml/ns/persistence

【Spring】Spring MVC原理及配置详解

1.Spring MVC概述: Spring MVC是Spring提供的一个强大而灵活的web框架.借助于注解,Spring MVC提供了几乎是POJO的开发模式,使得控制器的开发和测试更加简单.这些控制器一般不直接处理请求,而是将其委托给Spring上下文中的其他bean,通过Spring的依赖注入功能,这些bean被注入到控制器中. Spring MVC主要由DispatcherServlet.处理器映射.处理器(控制器).视图解析器.视图组成.他的两个核心是两个核心: 处理器映射:选择使用

01 - spring mvc 概述及配置DispatcherServlet

1.Spring mvc 基于model2实现,整体框架流程如(图片来自百度): ①web容器接收到http请求,若匹配DispatcherServlet的请求映射路径(web.xml),则容器会交给DispatcherServlet处理. ②DispatcherServlet根据请求的信息及handlerMapping(类似路由功能)的配置找到处理请求的Handler. ③Handler adapter对handler进行封装,使用统一的方法对Handler方法进行调用 ④处理器完成业务逻辑后

Spring mvc 拦截器配置

首先在web.xml中配置spring mvc入口:DispatcherServlet SpringMVC具有统一的入口DispatcherServlet,所有的请求都通过DispatcherServlet.    DispatcherServlet是前置控制器,配置在web.xml文件中的.拦截匹配的请求,Servlet拦截匹配规则要自已定义,把拦截下来的请求,依据某某规则分发到目标Controller来处理.  所以我们现在web.xml中加入以下配置: <servlet><!--s