content: package com.xuan.chapter12.authorization.server.config;
  
  import org.springframework.context.annotation.Bean;
  import org.springframework.context.annotation.Configuration;
  import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
  import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
  import org.springframework.security.access.vote.RoleHierarchyVoter;
  import org.springframework.security.access.vote.RoleVoter;
  import org.springframework.security.authentication.AuthenticationManager;
  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.config.http.SessionCreationPolicy;
  import org.springframework.security.core.userdetails.User;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.crypto.password.NoOpPasswordEncoder;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.security.provisioning.InMemoryUserDetailsManager;
  import org.springframework.security.web.authentication.logout.HttpStatusReturningLogoutSuccessHandler;
  
  /**
   * @author xuan
   * @create 2018-02-19 21:15
   **/
  
  @Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          // 在内存中配置两个用户
          InMemoryUserDetailsManager userDetailsManager = new InMemoryUserDetailsManager();
          userDetailsManager.createUser(User.withUsername("user_authorization_code").password("123456").authorities("USER").build());
          userDetailsManager.createUser(User.withUsername("user_password").password("123456").authorities("USER").build());
          auth.userDetailsService(userDetailsManager);
      }
  
      @Bean
      @Override
      public AuthenticationManager authenticationManagerBean() throws Exception {
          AuthenticationManager manager = super.authenticationManagerBean();
          return manager;
      }
  
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          // 允许访问/oauth授权接口
          http.csrf().disable()
                  .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
                  .and()
                  .requestMatchers().anyRequest()
                  .and()
                  .formLogin().permitAll()
                  .and()
                  .authorizeRequests()
                  .antMatchers("/oauth/*").permitAll();
  
      }
  
      @Bean
      public PasswordEncoder passwordEncoder() {
          return NoOpPasswordEncoder.getInstance();
      }
  
  }
  