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
相關文章
相關標籤/搜索