搭建可運行的spring-security多模塊工程項目java
工程目錄結構:
spring-security
├── spring-security-app-- app模塊代碼
├── spring-security-core-- 核心業務邏輯
├── spring-security-demo-- 樣例模塊邏輯
└── spring-security-web-- web模塊代碼mysql
使用idea建立spring-security父工程
選擇maven不勾選archetype
輸入maven座標
本地存儲位置
web
右鍵父工程:module選擇maven工程
輸入子工程的maven座標
選擇本地存儲路徑在父工程存儲路徑下redis
<dependencyManagement> <dependencies> <dependency> <groupId>io.spring.platform</groupId> <artifactId>platform-bom</artifactId> <version>Brussels-SR4</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.SR2</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies></dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins></build>
<!--引入依賴:不用寫version;dependencyManagement中的io.spring.platform;org.springframework.cloud作了統一管理\--> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-oauth2</artifactId> </dependency> <!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--三方登陸\--> <dependency> <groupId>org.springframework.social</groupId> <artifactId>spring-social-config</artifactId> </dependency> <dependency> <groupId>org.springframework.social</groupId> <artifactId>spring-social-core</artifactId> </dependency> <dependency> <groupId>org.springframework.social</groupId> <artifactId>spring-social-security</artifactId> </dependency> <dependency> <groupId>org.springframework.social</groupId> <artifactId>spring-social-web</artifactId> </dependency> <!--公共模塊\--> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> </dependency> <!--processor--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> </dependency> </dependencies>
<dependencies> <dependency> <groupId>com.yxm.security</groupId> <artifactId>spring-security-core</artifactId> <version>${project.version}</version> </dependency></dependencies>
<dependencies> <dependency> <artifactId>spring-security-web</artifactId> <groupId>com.yxm.security</groupId> <version>${project.version}</version> </dependency></dependencies>
<!--core中已經引入相關依賴了,咱們直接引入core--> <dependencies> <dependency> <groupId>com.yxm.security</groupId> <artifactId>spring-security-core</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.session</groupId> <artifactId>spring-session</artifactId> </dependency></dependencies>
### 3.運行項目
在spring-security-demo中
新建包:com.yxm.security
新建類:DemoApplication
添加註解:@SpringBootApplication、@RestController
添加main方法和測試方法:以下:spring
@SpringBootApplication @RestController public class DemoApplication { public static void main(String\[\] args) { SpringApplication.run(DemoApplication.class, args); } @RequestMapping("/hello") public String hello(){ return "hello spring security"; } }
運行報錯:Cannot determine embedded database driver class for database type NONEsql
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://127.0.0.1:3306/oa?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai data-username: root data-password: root
運行報錯:
關閉spring.sessionapache
spring: #關閉spring session否則會報錯 session: store-type: none
而後正常啓用demo項目:訪問:http://127.0.0.1:8088/hello
出現以下:
session
springBoot下spring-security默認配置下 訪問接口須要要求驗證:咱們關閉認證app
security: basic: enabled: false
訪問:http://127.0.0.1:8088/hello
輸出:hello spring security maven
咱們此時執行父工程maven命令:mvn clean package
而後咱們發現工程大小爲3kb不可執行
爲了讓demo能夠打成運行的jar包,咱們在demo的pom.xml文件裏面添加配置:
<build> <finalName>demo</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>1.3.3.RELEASE</version> <executions> <execution> <goals> <!--能夠把項目按照SpringBoot特有的方式來進行打包爲fileNmae的包--> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
以上工程搭建完畢。