Spring Security Identity hellospringsecurity (attached source)

Source: Internet
Author: User

In the previous document, the process and management of identity authentication in spring security were described in detail, and this article will tell you in practice how to use spring security for authentication in the easiest way.


The development environment is as follows:

JDK 1.7

Tomcat 7

Eclipse

Spring Security 3.2.5


The project directory structure is as follows:


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/58/E7/wKiom1TAYmXSnIpFAAGNOsTljug259.jpg "title=" Springsecurity05.png "alt=" Wkiom1taymxsnipfaagnostljug259.jpg "/>


1. Create a new Maven Project and recharge your own shoes for a child who is unfamiliar with Maven, and it's not a good idea to learn maven now.


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/58/E5/wKioL1TAX7KRtrmRAAIaDlk0914608.jpg "title=" Springsecurity01.png "alt=" Wkiol1tax7krtrmraaiadlk0914608.jpg "/>


2. Add related jar dependencies in Pom.xml.


<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>com.favccxx.favsecurity</groupid>   <artifactid>hellospringsecurity </artifactId>   <packaging>war</packaging>   <version> 0.0.1-snapshot</version>    <name>hellospringsecurity maven webapp </name>  <url>http://maven.apache.org</url>  <properties>   <spring.version>3.2.8.RELEASE</spring.version>  </properties>     <dependencies>       <dependency>           <groupid>junit</groupid>        <artifactid>junit</ artifactid>      <version>3.8.1</version>       <scope>test</scope>    </dependency>              <dependency><groupId>org.springframework< /groupid><artifactid>spring-core</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-test</ artifactid> --><!-- <version>${spring.version}</version> --><!--  </dependency> --><dependency>        < Groupid>org.freemarker</groupid>        <artifactid> freemarker</artifactid>         <version>2.3.20</version>    </ dependency>    <dependency><groupid>org.springframework</groupid>< Artifactid>spring-orm</artifactid><version>${spring.version}</version></dependency>              <dependency>      <groupId>org.springframework.security</groupId>    < artifactid>spring-security-web</artifactid>    <version>3.2.5.release</ version>   </dependency>   <dependency>     <groupId>org.springframework.security</groupId>    <artifactId> Spring-security-config</artifactid>    <version>3.2.5.release</version>   </dependency>     <dependency>            <groupId>jstl</groupId>            <artifactId>jstl</artifactId>            <version>1.2</version>       </dependency>   </dependencies>  <build>    <finalName>HelloSpringSecurity< /finalname>  </build></project>


3. Configure Web. XML to load the spring MVC configuration file and the spring Security configuration file when the container starts.


<?xml version= "1.0"  encoding= "UTF-8"? ><web-app id= "Hellospringsecurity"  version= " 2.4 " xmlns=" HTTP://JAVA.SUN.COM/XML/NS/J2EE "xmlns:xsi=" Http://www.w3.org/2001/XMLSchema-instance "xsi: schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >< Display-name>hello spring security</display-name><context-param><param-name> Contextconfiglocation</param-name><param-value>classpath:springsecurity.xml</param-value> </context-param><listener>         <listener-class >org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>          <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>           <servlet><servlet-name>springMVC</servlet-name><servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class> <init-param>              <param-name>contextconfiglocation</ Param-name>             <param-value >classpath:spring-context.xml</param-value>        </ Init-param>        <load-on-startup>1</load-on-startup> </servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/ </url-pattern></servlet-mApping></web-app> 


4. The Springsecurity.xml configuration file is as follows


<?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-3.2.xsdhttp://www.springframework.org/schema/security  http://www.springframework.org/schema/security/spring-security-3.2.xsd "><security:http  Auto-config= "true" ><security:intercept-url pattern= "/admin"  access= "ROLE_ADMIN"/>< Security:intercept-url pattern= "/confidential"  access= "Role_superadmin"/></security:http> <security:authentication-manager><security:authentication-provider><security:user-service> <security:user name= "Favccxx"  password= "Favccxx"  authorities= "ROLE_USER,ROLE_ADMIN"/>< Security:user name= "Super"  password= "SupeR " authorities=" Role_superadmin "/></security:user-service></security:authentication-provider > </security:authentication-manager></beans>


5.The Spring-context.xml configuration file is as follows


<?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:mvc=" Http://www.springframework.org/schema/mvc "xsi:schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/ context/spring-context-3.2.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/ Schema/mvc/spring-mvc-3.2.xsd "><mvc:annotation-driven></mvc:annotation-driven><context: Component-scan base-package= "Com.favccxx.favsecurity.web" ></context:component-scan>     <bean id= "Viewresolver"         class= " Org.springframework.web.servlet.view.UrlBasedViewResolver ">  &Nbsp;     <property name= "Viewclass"              value= "Org.springframework.web.servlet.view.JstlView"  />         <property name= "prefix"  value= "/WEB-INF/views"  / >        <property name= "suffix"  value= ". JSP"  / >    </bean></beans>


6. Create a new Hellospringsecuritycontroller.java file with the following code:


package com.favccxx.favsecurity.web;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 ; @Controllerpublic  class hellospringsecuritycontroller {@RequestMapping ("/hello") public  Modelandview hello () {Modelandview mav = new modelandview (); Mav.addObject ("title",   "Welcome - spring security hello world"); Mav.addobject ("Message",  "this  is welcome page! "); Mav.setviewname ("/hello"); Return mav;} @RequestMapping (value = {  "/",  "/welcome"  }, method =  Requestmethod.get)  public modelandview welcome ()  {modelandview mav = new  modelandview (); Mav.addobject ("title",  "welcome - spring security hello  World "); mAv.addobject ("message",  "this is welcome page!"); Mav.setviewname ("/hello");return mav; }  @RequestMapping (value =  "/admin",  method  = requestmethod.get)       public modelandview admin ()   {            modelandview mav = new  modelandview ();           mav.addobject ("title",  " Admin - spring security hello world ");           mav.addobject ("message",  "this is protected page!");           mav.setviewname ("/admin");          return mav;     }  }}


7. Create admin.jsp and hello.jsp separately under the/web-inf/views folder


<%@ page language= "java"  contenttype= "text/html;  Charset=utf-8 "    pageencoding=" UTF-8 "%><% @taglib  prefix=" C " uri="/http Java.sun.com/jsp/jstl/core "%>    <! doctype html>


<%@ 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" >


8. The system runs as follows


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/58/E7/wKiom1TAYYzRUp5pAAFnGxlExwo893.jpg "style=" width : 600px;height:180px; "title=" Springsecurity02.png "width=" "height=" "border=" 0 "hspace=" 0 "vspace=" 0 "alt=" Wkiom1tayyzrup5paafngxlexwo893.jpg "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/58/E5/wKioL1TAYmSRjfnQAAEXAbGEuwE726.jpg "style=" width : 600px;height:162px; "title=" Springsecurity03.png "width=" "height=" 162 "border=" 0 "hspace=" 0 "vspace=" 0 "alt=" Wkiol1taymsrjfnqaaexabgeuwe726.jpg "/>

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/58/E7/wKiom1TAYY2Cl1oaAAG_KHPe-pQ373.jpg "style=" width : 600px;height:174px; "title=" Springsecurity04.png "width=" "height=" 174 "border=" 0 "hspace=" 0 "vspace=" 0 "alt=" Wkiom1tayy2cl1oaaag_khpe-pq373.jpg "/>


Note: Click here to download the source code



This article is from the "Dust Wind with the Sky" blog, please be sure to keep this source http://favccxx.blog.51cto.com/2890523/1606889

Spring Security Identity hellospringsecurity (attached source)

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.