spring security 4 安全框架学习01

来源:互联网 发布:ibm公司软件 编辑:程序博客网 时间:2024/06/14 17:06

1、新建项目目录 security01-jc

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

pom.xml

[html] view plaincopy
  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.0</modelVersion>  
  4.   
  5.   <groupId>com.okttl</groupId>  
  6.   <artifactId>security</artifactId>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <packaging>war</packaging>  
  9.   <name>security</name>  
  10.  <properties>  
  11.         <!-- jdk 版本 -->  
  12.         <jdk.version>1.7</jdk.version>  
  13.         <!-- springframework (spring 框架)版本 -->  
  14.         <springframework.version>4.1.6.RELEASE</springframework.version>  
  15.         <!-- spring security (spring 安全框架)版本 -->  
  16.         <security.version>4.0.1.RELEASE</security.version>  
  17.         <servlet.version>3.1.0</servlet.version>  
  18.         <jsp.version>2.3.2-b01</jsp.version>  
  19.         <jstl.version>1.2</jstl.version>  
  20.     </properties>  
  21.       
  22.     <dependencyManagement>  
  23.         <dependencies>  
  24.           <dependency>  
  25.             <!-- 不需要再定义所有子模块版本  -->  
  26.             <groupId>org.springframework</groupId>  
  27.             <artifactId>spring-framework-bom</artifactId>  
  28.             <version>${springframework.version}</version>  
  29.             <type>pom</type>  
  30.             <scope>import</scope>  
  31.           </dependency>  
  32.         </dependencies>  
  33.     </dependencyManagement>  
  34.       
  35.     <dependencies>  
  36.         <!-- springframework 框架的引入 -->  
  37.         <dependency>  
  38.             <groupId>org.springframework</groupId>  
  39.             <artifactId>spring-core</artifactId>   
  40.         </dependency>  
  41.         <dependency>  
  42.             <groupId>org.springframework</groupId>  
  43.             <artifactId>spring-context-support</artifactId>           
  44.         </dependency>  
  45.         <!-- spring mvc 的引用 -->  
  46.         <dependency>  
  47.             <groupId>org.springframework</groupId>  
  48.             <artifactId>spring-webmvc</artifactId>  
  49.         </dependency>  
  50.           
  51.         <!-- spring 安全框架  -->  
  52.         <dependency>  
  53.             <groupId>org.springframework.security</groupId>  
  54.             <artifactId>spring-security-web</artifactId>  
  55.             <version>${security.version}</version>  
  56.         </dependency>  
  57.         <dependency>  
  58.             <groupId>org.springframework.security</groupId>  
  59.             <artifactId>spring-security-config</artifactId>  
  60.             <version>${security.version}</version>  
  61.         </dependency>  
  62.           
  63.         <!-- Servlet 依赖 -->  
  64.         <dependency>  
  65.             <groupId>javax.servlet</groupId>  
  66.             <artifactId>javax.servlet-api</artifactId>  
  67.             <version>${servlet.version}</version>  
  68.             <scope>provided</scope>  
  69.         </dependency>  
  70.         <dependency>  
  71.             <groupId>javax.servlet.jsp</groupId>  
  72.             <artifactId>javax.servlet.jsp-api</artifactId>  
  73.             <version>${jsp.version}</version>  
  74.             <scope>provided</scope>  
  75.         </dependency>  
  76.         <dependency>  
  77.             <groupId>javax.servlet</groupId>  
  78.             <artifactId>jstl</artifactId>  
  79.             <version>${jstl.version}</version>  
  80.         </dependency>  
  81.     </dependencies>  
  82.     <build>  
  83.         <plugins>  
  84.             <plugin>  
  85.                 <groupId>org.apache.maven.plugins</groupId>  
  86.                 <artifactId>maven-compiler-plugin</artifactId>  
  87.                 <version>3.3</version>  
  88.                 <configuration>  
  89.                     <source>${jdk.version}</source>  
  90.                     <target>${jdk.version}</target>  
  91.                     <!-- java 的 Xlint 命令,all 启用所有告警-->  
  92.                     <compilerArgument>-Xlint:all</compilerArgument>  
  93.                     <showWarnings>true</showWarnings>  
  94.                     <showDeprecation>true</showDeprecation>  
  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 plaincopy
  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 plaincopy
  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 plaincopy
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"  
  3.     xmlns:c="http://java.sun.com/jsp/jstl/core" version="2.0">  
  4.   
  5.   <jsp:directive.page contentType="text/html" pageEncoding="UTF-8" />  
  6.   <jsp:output omit-xml-declaration="true" />  
  7.   <jsp:output doctype-root-element="HTML"  
  8.               doctype-system="about:legacy-compat" />  
  9. <html>  
  10.   <head>  
  11.     <title>Hello Security</title>  
  12.   
  13.   </head>  
  14.   
  15.   <body>  
  16.     <div class="container">  
  17.       <h1>This is secured!</h1>  
  18.       <p>  
  19.         Hello <b><c:out value="${pageContext.request.remoteUser}"/></b>  
  20.       </p>  
  21.       
  22.     </div>  
  23.   </body>  
  24. </html>  
  25. </jsp:root>  

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

0 0
原创粉丝点击