Spring Boot - 自定义配置

1.实现自定义的配置类

    在前面一篇文章提到了Spring4.0的条件化注解,咱们能够查看@ConditionalOnBean这样的自动化配置类就知道能够实现和继承相对应的接口和类,并使用对应的注解注入到容器中,既可实现自定义配置覆盖自动化配置(确切的说是自定义配置先生效,Spring Boot在扫描处理自动化配置时由于条件不知足而为生效),举例:java

package com.example.demo.readinglist;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Autowired
  private ReaderRepository readerRepository;
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .authorizeRequests()
        .antMatchers("/").access("hasRole('READER')")
        .antMatchers("/**").permitAll()
      .and()
      .formLogin()
        .loginPage("/login")
        .failureUrl("/login?error=true");
  }
  
  @Override
  protected void configure(
              AuthenticationManagerBuilder auth) throws Exception {
    auth
      .userDetailsService(new UserDetailsService() {
        @Override
        public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException {
          UserDetails userDetails = readerRepository.findOne(username);
          if (userDetails != null) {
            return userDetails;
          }
          throw new UsernameNotFoundException("User '" + username + "' not found.");
        }
      });
  }

}

上面的类继承WebSecurityConfigurerAdapter,让它具有了Spring Security功能,而使用@EnableWebSecurity注解注入了WebSecurityConfiguration 类型的 Bean,所以Spring Boot在处理SpringBootWebSecurityConfiguration自动化配置类时由于不知足@ConditionalOnMissingBean(WebSecurityConfiguration.class)条件而被忽略mysql

2.经过属性文件外置配置

    Spring Boot提供了很是多的属性让其覆盖默认配置,是一种更轻量级的实现,让开发者不用为了一点小功能就去推翻整个自动化配置提供的强大功能。Spring Boot 真的是太好了。web

    方式不少,罗列一下,本文只讲一下application.properties或者application.yml,其余经过命令行,环境变量等方式会说起到:spring

    1.命令行参数sql

    2.java:comp/env里的jndi属性缓存

    3.JVM系统属性服务器

    4.操做系统环境变量app

    5.随机生成的待random.*前缀的属性(没搞懂怎么使用,下面去初略了解一下dom

    6.应用程序之外的application.properties application.ymlide

    7.应用程序之内的application.properties application.yml

    8.经过@PropertiesSource标注的属性源

    9.默认属性

    举例:

    1.打开Spring Boot 日志

logging.path=/var/logs/
logging.file=readingList.log
logging.level.root=WARN
logging.level.org.springframework.security=DEBUG

    2.禁用模板缓存

spring.thymeleaf.cache:false

    3.修改嵌入式服务器端口

server.port=8443

    4.配置数据源

spring.datasource.url:jdbc:mysql://locaohost/readinglist
spring.datasource.username:admin
spring.datasource.password:admin1234
相关文章
相关标签/搜索