Spring Security 4.1 Add authentication code and dynamic rights Management

Source: Internet
Author: User

Originally wanted to step-by-step gradually in-depth, but suddenly there are other things, can only be a one-time demonstration of some advanced functions

The way to add a captcha there are a lot of related articles on the Internet, I use the way here is to replace the standard filter chain Usernamepasswordauthenticationfilter, the verification code is generated not here to discuss the core content, So directly using code simulation in the session generated a verification code, dynamic permissions to replace the permissions of the XML configuration, is in front of the filter_security_interceptor to add a custom filter to implement.

Add the jar package you want, the Pom file is as follows:

<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/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>pers.homer.test</groupId> <artifactid>spring</ artifactid> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name> Spring Maven webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependenc y> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8 .1</version> <scope>test</scope> </dependency> <dependency> <groupi D>org.springframework</groupid> <artifactId>spring-webmvc</artifactId> <version&gt
;4.3.1.release</version> </dependency>    <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactid>jackson
        -core</artifactid> <version>2.8.0</version> </dependency> <dependency> 
        <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupid>javax. Servlet</groupid> <artifactId>jstl</artifactId> <version>1.2</version> & Lt;/dependency> <dependency> <groupId>org.springframework.security</groupId> < Artifactid>spring-security-web</artifactid> <version>4.1.1.RELEASE</version> </depende ncy> <dependency> <groupId>org.springframework.security</groupId> <artifactid& Gt;spring-security-config</artifactid> <version>4.1.1.RELEASE</version> </dependency> </dependencies>

 <build> <finalName>spring</finalName> </build> </project>

Project structure

Configuration of 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>servlet 3.0 Web APPLICATION&LT;/DISPL ay-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-fil e-list> <context-param> <param-name>contextConfigLocation</param-name> <param-val Ue>/web-inf/applicationcontext.xml,/web-inf/spring-security.xml</param-value> </context-param> &L t;listener> <listener-class> Org.springframework.web.context.ContextLoaderListener &L t;/listener-class> </listener> <filter> <filter-name>springsecurityfilterchain</f Ilter-name> <filter-class>org.springframework.web.filter.delegatingfilterproxy</filter-class> </filter> < Filter-mapping> <filter-name>springSecurityFilterChain</filter-name> <url-pattern>/*& Lt;/url-pattern> </filter-mapping> <!--character Set filter--> <filter> <filter-name>c Haracterencodingfilter</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-mapping> < Filter-name>characterencodingfilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>spring</servlet- name> <servlet-class> Org.springframework.web.servlet.DispatcherServlet </servlet-
        class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>spring</servlet-name> <url-pattern>/</url-pattern> </servlet-mappin  G> </web-app>

Spring's default profile, Applicationcontext.xml, enables annotations and is temporarily unused to AOP.

<?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:p= "http://www.springframework.org/schema/p"
    xmlns:context= "Http://www.springframework.org/schema/context"
    xmlns:aop= "http:// WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "
    xsi:schemalocation="
Http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/ Context
http://www.springframework.org/schema/context/spring-context.xsd
http:// WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP  
http://www.springframework.org/schema/aop/spring-aop.xsd 
">
    <!--scan annotation file-->
    <context:component-scan base-package= "Pers.homer.test"/>
    <!-- AOP automatic annotation 
    <aop:aspectj-autoproxy/>
    -->

</beans>

The configuration file for spring Web MVC spring-servlet.xml

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: context= "Http://www.springframework.org/schema/context" xmlns:mvc= "Http://www.springframework.org/schema/mvc" XM Lns: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/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 "> <!--scan annotation file--> <context:component-scan ba Se-package= "Pers.homer.test"/> <!--boot annotation--> <mvc:annotation-driven/> <!--<bean ID = "Stringhttpmessageconverter" class= "Org.springframework.http.converter.StringHttpMessageConverter"/> < Bean id= "Jasonhttpmessageconverter" class= ORG.SPRINGFRamework.http.converter.json.MappingJackson2HttpMessageConverter "> <property name=" supportedmediatypes " 
        > <list> <value>text/html;charset=UTF-8</value> </list>
            </property> </bean> <mvc:annotation-driven> <mvc:message-converters>   
        <ref bean= "Stringhttpmessageconverter"/> <ref bean= "Jasonhttpmessageconverter"/> </mvc:message-converters> </mvc:annotation-driven>--> <!--static resource mapping--> <m Vc:resources mapping= "/javascript/**" location= "/static/javascript/"/> <mvc:resources mapping= "/styles/**" Lo cation= "/static/styles/"/> <mvc:resources mapping= "/images/**" location= "/static/images/"/> <!--make Handling static files with the default servlet--> <mvc:default-servlet-handler/> <!--view name to file mappings--> <bean class = "Org.springframework.web.servLet.view.InternalResourceViewResolver "> <property name=" viewclass "value=" org.springframework.we B.servlet.view.jstlview "/> <property name= prefix" value= "/view/"/> <property name= "suffix" Value= ". jsp"/> </bean> </beans>

Spring Security's configuration file, 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/XM Lschema-instance "xsi:schemalocation=" Http://www.springframework.org/schema/beans Http://www.springfram Ework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/security HTTP://WWW.SPRINGFR Amework.org/schema/security/spring-security-4.1.xsd "> <debug/> 

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.