Spring Boot 搭建 Spring Security环境

作者: 小小俊子 | 来源:发表于2017-06-05 14:35 被阅读223次

创建Spring Boot的Maven项目

  利用 Spring Boot 可以简化开发环境的搭建,特别是对于一些新知识点的学习,保证学习的时候不被环境及其它无关操作所影响.Spring Security项目官网上为 Security 项目官方提供了Demo ,但 Download 下来之后发现有些问题,并不能够直接运行,造成这样的原因是POM 中有些错误.所以我们可以自己创建一个简单的 Maven 项目,对其做加法,把需要的依赖,一个一个的添加

添加项目依赖

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

添加视解析器

  之所以把视图解析器单独解释,是因为 Spring Boot 中的默认视力解析器是
thymeleaf,所说 thymeleaf 有很多好处,但我没有具体了解过.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>

修改配置文件

  Spring Boot为我们的项目默认设置了许多配置,当我们需要修改的时候,我们只需要按其要求添加配置文件,并把需要修改的内容写入配置文件就行.下面我们采用属性文件修改配置的方式,把项目的发布路径修改一下.在资源文件包下面添加属性文件 application.properties,并修改其内容.

server.context-path=/sec

  只需要加入一句就可以了,这样我们访问程序的时候就不是localhost:8080/,而是需要在最后一个根号后面加入sec,多个项目同时支行的时候方便区分.采用属性文件的原因是我不懂官方中采用的<code>yml</code>形式的配置_所以我把<code>application.yml</code>这个配置文件给去了.

导入Spring官方代码

   Securyit 的官方Demo,下载之后解压就可以得到源码,方便我们把官方源码导入到项目中,具体路径在 SPRING_SECURITY_HOME/samples/boot/helloworld 下.

导入Security的配置代码

  如果我们不导入这个配置文件,那么Spring下面的两个类已经为我们提供了默认的配置,两个类他别是
org.springframework.boot.autoconfigure.security.SecurityProperties 和org.springframework.boot.autoconfigure.security.SpringBootWebSecurityConfiguration,但是这两个类都在Spring Boot下面,若项目不是采用Spring Boot的,那么就没有作用.若我们没有自定义 Security 的配置文件,那么程序运行的时候默认采用的用户名就是 user ,密码在Spring Boot 程序运行的时候显示在控制台.Security的配置代码如:

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                    .antMatchers("/css/**", "/index").permitAll()
                    // user下面的资源需要拥有"USER"角色方可访问
                    .antMatchers("/user/**").hasRole("USER")
                    .and()
                .formLogin()
                    .loginPage("/login")
                    .failureUrl("/login-error");
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .withUser("user").password("password").roles("USER");
    }
}

  可以在这个配置文件中自定义安全配置,如用户名和资源文件 etc,同时在这个地方对不同的资源或角色进行授权操作.

结果

  把官方Demo中的代码导入之后,IDE中显示如图所示.

工程项目结构图

相关文章

网友评论

    本文标题:Spring Boot 搭建 Spring Security环境

    本文链接:https://www.haomeiwen.com/subject/yovifxtx.html