实现达到的效果:
1、深入理解Spring Security及相关框架的原理、功能和代码。
2、可以基于Spring Security及相关框架独立开发认证授权相关功能。
3、掌握抽象和封装的常用技巧,可以编写可重用的模块供他人使用。
具体maven项目的构建在这里不多说了,非常简单。可以参考这篇文章IntelliJ IDEA创建maven多模块项目 ,按照上面的代码结构构建。搭建好的项目如下:
immoc-security 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.immoc.securitygroupId>
<artifactId>immoc-seccurityartifactId>
<packaging>pompackaging>
<version>1.0-SNAPSHOTversion>
<properties>
<imooc.security.version>1.0-SNAPSHOTimooc.security.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platformgroupId>
<artifactId>platform-bomartifactId>
<version>Brussels-SR6version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>Dalston.SR5version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-compiler-pluginartifactId>
<version>2.3.2version>
<configuration>
<source>1.8source>
<target>1.8target>
<encoding>UTF-8encoding>
configuration>
plugin>
plugins>
build>
<modules>
<module>immoc-security-codemodule>
<module>immoc-security-appmodule>
<module>immoc-security-browsermodule>
<module>immoc-security-demomodule>
modules>
project>
immoc-security-code 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<artifactId>immoc-security-codeartifactId>
<parent>
<artifactId>immoc-seccurityartifactId>
<groupId>com.immoc.securitygroupId>
<version>1.0-SNAPSHOTversion>
parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-oauth2artifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-redisartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-jdbcartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>org.springframework.socialgroupId>
<artifactId>spring-social-configartifactId>
dependency>
<dependency>
<groupId>org.springframework.socialgroupId>
<artifactId>spring-social-coreartifactId>
dependency>
<dependency>
<groupId>org.springframework.socialgroupId>
<artifactId>spring-socail-securityartifactId>
dependency>
<dependency>
<groupId>org.springframework.socialgroupId>
<artifactId>spring-social-webartifactId>
dependency>
<dependency>
<groupId>commons-langgroupId>
<artifactId>commons-langartifactId>
dependency>
<dependency>
<groupId>commons-collectionsgroupId>
<artifactId>commons-collectionsartifactId>
dependency>
<dependency>
<groupId>commons-beanutilsgroupId>
<artifactId>commons-beanutilsartifactId>
dependency>
dependencies>
project>
immoc-security-app 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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>immoc-seccurityartifactId>
<groupId>com.immoc.securitygroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>immoc-security-appartifactId>
<dependencies>
<dependency>
<groupId>com.immoc.securitygroupId>
<artifactId>immoc-security-demoartifactId>
<version>${imooc.security.version}version>
dependency>
dependencies>
project>
immoc-security-browser 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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>immoc-seccurityartifactId>
<groupId>com.immoc.securitygroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>immoc-security-browserartifactId>
<dependencies>
<dependency>
<groupId>com.immoc.securitygroupId>
<artifactId>immoc-security-codeartifactId>
<version>${imooc.security.version}version>
dependency>
<dependency>
<groupId>org.springframework.sessiongroupId>
<artifactId>spring-sessionartifactId>
dependency>
dependencies>
project>
immoc-security-demo 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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>immoc-seccurityartifactId>
<groupId>com.immoc.securitygroupId>
<version>1.0-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>immoc-security-demoartifactId>
<dependencies>
<dependency>
<groupId>com.immoc.securitygroupId>
<artifactId>immoc-security-browserartifactId>
<version>${imooc.security.version}version>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>1.3.3.RELEASEversion>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
<finalName>demofinalName>
build>
project>
初步环境已搭建好,下面编写hello测试。
immoc-security-demo模块下新建DemoApplication类测试:
package com.immoc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by zhixinhua on 18/1/20.
*/
@SpringBootApplication
@RestController
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
@GetMapping("/hello")
public String hello(){
return "hello spring security!";
}
}
好了,总于可以跑起来了试试……
ok,那就配置数据库链接吧,新建application.properties(噢噢,当然要先创建数据库,这里就不详细说了)
application.properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springsecurity?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
问题二:由于在immoc-security-browser模块下加入了 集群环境下session的管理,所以报没有配置spring session存储管理。
目前我们暂时没有用到,先关闭。在application.properties中添加:
spring.session.store-type=none
到此,终于控制台没有报错了。访问http://localhost:7070/hello ,给我们弹出身份验证。
………用户名、密码是什么?????? 我现在也不知道,好吧!关掉它!!!
在application.properties中添加关闭基本的鉴权:
security.basic.enabled=false
完整的application.properties
#连数据连接配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springsecurity?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
#不需将session放入redis
spring.session.store-type=none
#关闭基本的鉴权
security.basic.enabled=false
#配置端口
server.port=7070
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<version>1.3.3.RELEASEversion>
<executions>
<execution>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
plugin>
plugins>
<finalName>demofinalName>
build>
IntelliJ IDEA运行maven程序可参考IntelliJ IDEA 14 如何运行maven程序。
打包后可以看到immoc-security-demo target目录下多了demo.jar和demo.jar.original
切换到demo.jar所在目录,执行“java -jar demo.jar“,启动项目。
启动成功后即可访问。
项目搭建已完成,待续。。。。。。。
demo源码