UserDetailsS​​ervice嵌入在Spring-Security配置类中

时间:2014-11-24 13:41:09

标签: spring spring-mvc spring-security

在我的春季项目中,我通常会在此课程中提供控制访问权限:

@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  @Autowired
  private UserDetailsService userDetailsService;

  @Autowired
  private PasswordEncoder passwordEncoder;

  @Autowired
  private AuthenticationManagerBuilder auth;

  @Autowired
  private PermissionEvaluator permissionEvaluator;

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http
      .csrf()
        .disable()
      .authorizeRequests()
        .antMatchers("/**", "/static/**").permitAll()
        .anyRequest().authenticated()
        .and()
      .formLogin()
        .loginPage("/signin")
        .loginProcessingUrl("/login").permitAll()
        .usernameParameter("login")
        .passwordParameter("senha")
        .defaultSuccessUrl("/home", true)
        .and()
      .rememberMe()
        .key("remember-me")
        .and()
      .logout()
        .logoutUrl("/logout")
        .logoutSuccessUrl("/");
  }

  @Override
  public void configure(WebSecurity web) throws Exception {
    DefaultWebSecurityExpressionHandler handler = new DefaultWebSecurityExpressionHandler();
    handler.setPermissionEvaluator(permissionEvaluator);

    web
      .expressionHandler(handler);
  }

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
      .userDetailsService(userDetailsService)
      .passwordEncoder(passwordEncoder);
  }

  @Bean
  @Override
  public AuthenticationManager authenticationManagerBean() throws Exception {
    return auth.getOrBuild();
  }
}

通常,我使用分离的类实现bean UserDetailsS​​ervice,PasswordEncoder和PermissionEvaluator。现在,我正在寻找一种在上面的类中实现这个bean的方法,作为内部类或方法。

任何人都知道这是否可行,并且可以为我提供一个这样的例子的链接吗?

0 个答案:

没有答案