Initial knowledge of Spring Security

Source: Internet
Author: User

This article refers to or turns from: http://haohaoxuexi.iteye.com/blog/2154299

1. New Spring Security configuration file Spring-security.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:security= "Http://www.springframework.org/schema/security"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 "> <!--HTTP elements are used to define web-related permission controls.  - <!--Intercept-url defines a rule for permission control. The Pattern property indicates which URLs we will have permission to control, or it can be a regular expression, as the above notation means that we will control all URLs, and the Access property indicates what permissions are required to request the corresponding URL. The default configuration should be a comma-delimited list of roles that the requesting user can successfully access by simply owning one of the roles. The "Role_user" here indicates that the requested user should have a role_user role. The "Role_" prefix is a token that prompts spring to use role-based checking. -
<!--Note: auto-config= "true" when Springsecurity found no login back to automatically create landing page- - <security:httpAuto-config= "true"> <Security:intercept-urlpattern="/**"Access= "Role_user"/> </security:http> <!--using AuthenticationManager for authentication-related configurations - <!--the Authentication-manager element specifies a authenticationmanager, It requires a authenticationprovider (corresponding to the Authentication-provider element) for true authentication - <Security:authentication-manager> <Security:authentication-provider> <Security:user-service> <Security:username= "User"Password= "User"authorities= "Role_user"/> <Security:username= "Admin"Password= "Admin"authorities= "Role_user, role_admin"/> </Security:user-service> </Security:authentication-provider> </Security:authentication-manager></Beans>

2. In the Web. xml file, specify it as the initial profile of spring by Context-param and tell spring to load the configuration file.

< Context-param >        < Param-name >contextconfiglocation</param-name>        <  Param-value>/web-inf/applicationcontext.xml,/web-inf/spring-security.xml</ Param-value >    </ Context-param >

3. Configure filter to submit the request to spring security for processing

<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>

The final Web. xml file is as follows (SPRINGMVC project, therefore with Spring MVC configuration)

<?XML version= "1.0" encoding= "UTF-8"?><Web-appxmlns= "Http://xmlns.jcp.org/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"version= "3.1">   <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> <context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>/web-inf/applicationcontext.xml,/web-inf/spring-security.xml</Param-value>    </context-param>    <Listener>        <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>    </Listener>    <servlet>        <Servlet-name>Dispatcher</Servlet-name>        <Servlet-class>Org.springframework.web.servlet.DispatcherServlet</Servlet-class>        <Load-on-startup>1</Load-on-startup>    </servlet>    <servlet-mapping>        <Servlet-name>Dispatcher</Servlet-name>        <Url-pattern>*.do</Url-pattern>    </servlet-mapping></Web-app>

4, start the project such as:

User name, password login can be configured using Security:user

Initial knowledge of Spring Security

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.