spring security 4 安全框架学习01

1、新建项目目录 security01-jc

2、在  security01-jc 新建pom.xml文件

pom.xml

[html]  view plain copy
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.   <modelVersion>4.0.0modelVersion>  
  4.   
  5.   <groupId>com.okttlgroupId>  
  6.   <artifactId>securityartifactId>  
  7.   <version>0.0.1-SNAPSHOTversion>  
  8.   <packaging>warpackaging>  
  9.   <name>securityname>  
  10.  <properties>  
  11.           
  12.         <jdk.version>1.7jdk.version>  
  13.           
  14.         <springframework.version>4.1.6.RELEASEspringframework.version>  
  15.           
  16.         <security.version>4.0.1.RELEASEsecurity.version>  
  17.         <servlet.version>3.1.0servlet.version>  
  18.         <jsp.version>2.3.2-b01jsp.version>  
  19.         <jstl.version>1.2jstl.version>  
  20.     properties>  
  21.       
  22.     <dependencyManagement>  
  23.         <dependencies>  
  24.           <dependency>  
  25.               
  26.             <groupId>org.springframeworkgroupId>  
  27.             <artifactId>spring-framework-bomartifactId>  
  28.             <version>${springframework.version}version>  
  29.             <type>pomtype>  
  30.             <scope>importscope>  
  31.           dependency>  
  32.         dependencies>  
  33.     dependencyManagement>  
  34.       
  35.     <dependencies>  
  36.           
  37.         <dependency>  
  38.             <groupId>org.springframeworkgroupId>  
  39.             <artifactId>spring-coreartifactId>   
  40.         dependency>  
  41.         <dependency>  
  42.             <groupId>org.springframeworkgroupId>  
  43.             <artifactId>spring-context-supportartifactId>           
  44.         dependency>  
  45.           
  46.         <dependency>  
  47.             <groupId>org.springframeworkgroupId>  
  48.             <artifactId>spring-webmvcartifactId>  
  49.         dependency>  
  50.           
  51.           
  52.         <dependency>  
  53.             <groupId>org.springframework.securitygroupId>  
  54.             <artifactId>spring-security-webartifactId>  
  55.             <version>${security.version}version>  
  56.         dependency>  
  57.         <dependency>  
  58.             <groupId>org.springframework.securitygroupId>  
  59.             <artifactId>spring-security-configartifactId>  
  60.             <version>${security.version}version>  
  61.         dependency>  
  62.           
  63.           
  64.         <dependency>  
  65.             <groupId>javax.servletgroupId>  
  66.             <artifactId>javax.servlet-apiartifactId>  
  67.             <version>${servlet.version}version>  
  68.             <scope>providedscope>  
  69.         dependency>  
  70.         <dependency>  
  71.             <groupId>javax.servlet.jspgroupId>  
  72.             <artifactId>javax.servlet.jsp-apiartifactId>  
  73.             <version>${jsp.version}version>  
  74.             <scope>providedscope>  
  75.         dependency>  
  76.         <dependency>  
  77.             <groupId>javax.servletgroupId>  
  78.             <artifactId>jstlartifactId>  
  79.             <version>${jstl.version}version>  
  80.         dependency>  
  81.     dependencies>  
  82.     <build>  
  83.         <plugins>  
  84.             <plugin>  
  85.                 <groupId>org.apache.maven.pluginsgroupId>  
  86.                 <artifactId>maven-compiler-pluginartifactId>  
  87.                 <version>3.3version>  
  88.                 <configuration>  
  89.                     <source>${jdk.version}source>  
  90.                     <target>${jdk.version}target>  
  91.                       
  92.                     <compilerArgument>-Xlint:allcompilerArgument>  
  93.                     <showWarnings>trueshowWarnings>  
  94.                     <showDeprecation>trueshowDeprecation>  
  95.                 configuration>  
  96.             plugin>  
  97.         plugins>  
  98.     build>  
  99. project>  

3、在security01-jc 目录内新建 src/main/java 目录,在此目录下新建包(java 开发人员都知道也就是目录) 和类我的目录结构如下 :

com.okttl.security

在此包下新建两个类SecurityConfig.java 和 SecurityWebApplicationInitializer.java

SecurityConfig.java  源码

[java]  view plain copy
  1. package com.okttl.security;  
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;  
  4. import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;  
  5. import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;  
  6. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;  
  7.   
  8. @EnableWebSecurity  
  9. public class SecurityConfig extends WebSecurityConfigurerAdapter {  
  10.       
  11.     @Autowired  
  12.     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {  
  13.         auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");  
  14.     }  
  15. }  

SecurityWebApplicationInitializer.java 源码

[java]  view plain copy
  1. package com.okttl.security;  
  2.   
  3. import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;  
  4.   
  5. public class SecurityWebApplicationInitializer extends  
  6.         AbstractSecurityWebApplicationInitializer {  
  7.     public SecurityWebApplicationInitializer(){  
  8.         super(SecurityConfig.class);  
  9.     }  
  10. }  

4、在security01-jc/src/main 目录内新建webapp目录。 在此目录内新建index.jsp 文件

index.jsp 代码:

[plain]  view plain copy
  1.   
  2.     xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.0">  
  3.   
  4.     
  5.     
  6.   
  7.               doctype-system="about:legacy-compat" />  
  8.   
  9.     
  10.     Hello Security  
  11.   
  12.     
  13.   
  14.     
  15.       
  16.       

    This is secured!

      
  17.       

      

  18.         Hello   
  19.       

      
  20.       
  21.     
  
  •     
  •   
  •   

  • 到此项目已经可以运行了。注意你的电脑上的jdk 要与 jdk.version 版本一致

    你可能感兴趣的:(spring security 4 安全框架学习01)