Spring Security开发安全的REST服务之项目搭建

前言

实现达到的效果:
1、深入理解Spring Security及相关框架的原理、功能和代码。
2、可以基于Spring Security及相关框架独立开发认证授权相关功能。
3、掌握抽象和封装的常用技巧,可以编写可重用的模块供他人使用。

涉及的三个spring项目:
Spring Security开发安全的REST服务之项目搭建_第1张图片

项目搭建

1、代码结构

Spring Security开发安全的REST服务之项目搭建_第2张图片

2、构建maven项目

具体maven项目的构建在这里不多说了,非常简单。可以参考这篇文章IntelliJ IDEA创建maven多模块项目 ,按照上面的代码结构构建。搭建好的项目如下:
Spring Security开发安全的REST服务之项目搭建_第3张图片

3、配置pom依赖
3.1、配置主模块依赖

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>
3.2、子模块immoc-security-code的依赖

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>
3.3、子模块immoc-security-app依赖

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>
3.4、子模块immoc-security-browser依赖

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>
3.5、子模块immoc-security-demo依赖

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>

4、Hello Spring Security

初步环境已搭建好,下面编写hello测试。
immoc-security-demo模块下新建DemoApplication类测试:
Spring Security开发安全的REST服务之项目搭建_第4张图片

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!";
    }
}

好了,总于可以跑起来了试试……

很遗憾,跑起来就抛出如下错误:
问题一:没有配置数据库链接
Spring Security开发安全的REST服务之项目搭建_第5张图片

ok,那就配置数据库链接吧,新建application.properties(噢噢,当然要先创建数据库,这里就不详细说了)
Spring Security开发安全的REST服务之项目搭建_第6张图片
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 ,给我们弹出身份验证。
Spring Security开发安全的REST服务之项目搭建_第7张图片

………用户名、密码是什么?????? 我现在也不知道,好吧!关掉它!!!
在application.properties中添加关闭基本的鉴权:

security.basic.enabled=false

重新启动,大功告成!!
Spring Security开发安全的REST服务之项目搭建_第8张图片

完整的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

5、也可以使用maven把项目打包发布

5.1、immoc-security-demo的pom添加打包插件
<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>
5.2、maven 命令clean package运行项目

IntelliJ IDEA运行maven程序可参考IntelliJ IDEA 14 如何运行maven程序。

打包后可以看到immoc-security-demo target目录下多了demo.jar和demo.jar.original
Spring Security开发安全的REST服务之项目搭建_第9张图片

5.3、执行demo.jar

切换到demo.jar所在目录,执行“java -jar demo.jar“,启动项目。
Spring Security开发安全的REST服务之项目搭建_第10张图片

启动成功后即可访问。


项目搭建已完成,待续。。。。。。。

demo源码

你可能感兴趣的:(java,spring,security)