Spring Boot不使用UserDetailsS​​ervice

时间:2015-08-20 15:48:18

标签: spring spring-mvc jpa

我正在尝试使用注释为JPA身份验证配置Spring Boot 1.2.5应用程序,它似乎总是使用内存提供程序。

申请表:

@EnableWebMvc
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class ClubBooksApplication {

  protected final Logger logger = LoggerFactory.getLogger(getClass());

  public static void main(String[] args) {
    SpringApplication.run(ClubBooksApplication.class, args);
  }

}

WebSecurityConfigurerAdapter。我玩过订单,但似乎总是配置内存提供商。我觉得我可能错过了一段配置,但这种模式与我在搜索中找到的样本相匹配。

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
//@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)   // After in memory
//@Order(SecurityProperties.IGNORED_ORDER)           // Before in memory
//@Order(SecurityProperties.BASIC_AUTH_ORDER)        // Not unique
@Order(SecurityProperties.BASIC_AUTH_ORDER - 50)   // Before in memory
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

  private  PasswordEncoder    passwordEncoder;
  protected final Logger      logger = LoggerFactory.getLogger(getClass());

  @Autowired
  private UserDetailsService userDetailsService;

  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    logger.info(String.format("configure AuthenticationManagerBuilder: %s", userDetailsService));

    super.configure(auth);

    auth.userDetailsService(userDetailsService)
        .passwordEncoder(passwordEncoder());
  }

这是日志输出。在显示生成的密码之前,您可以看到它正在配置UserDetailsService。基于我对代码的挖掘,如果没有配置其他提供程序但是设置UserDetailsService配置DAO提供程序,它似乎只配置内存提供程序。

2015-08-20 11:19:24.187  INFO 42332 --- [ost-startStop-1] yConfig$$EnhancerBySpringCGLIB$$c647e8e8 : configure AuthenticationManagerBuilder: com.wstrater.server.clubBooks.server.service.impl.UserLoginDetailServiceImpl@46f0f40a
2015-08-20 11:19:24.226  INFO 42332 --- [ost-startStop-1] yConfig$$EnhancerBySpringCGLIB$$c647e8e8 : passwordEncoder
2015-08-20 11:19:24.410  INFO 42332 --- [ost-startStop-1] b.a.s.AuthenticationManagerConfiguration : 

Using default security password: 838a7ab0-3bd0-4e87-94ca-de2dfd34b965

2015-08-20 11:19:24.526  INFO 42332 --- [ost-startStop-1] yConfig$$EnhancerBySpringCGLIB$$c647e8e8 : configure HttpSecurity

我在应用程序中包含了Actuator,当我尝试访问http://localhost:8080/mappings时,尽管配置了基于表单的身份验证,但仍会提示BasicAuth。用户/生成的密码适用于BasicAuth。我的UserDetailsService实现未被调用。

配置HttpSecurity。在创建内存提供程序并显示生成的密码后调用此方法,因此我怀疑它是否会影响提供程序配置。我发现有趣的一件事是,尽管指定了BasicAuth,但仍会提示我formLogin()。我提出这个问题,因为我也遇到了映射控制器的问题。我认为这是无关的,但我知道什么。

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    logger.info("configure HttpSecurity");

    super.configure(http);

    http.authorizeRequests()
        .antMatchers("/", "/public/**")
          .permitAll()
        .antMatchers("/rest/**")
          .authenticated()
        .antMatchers("/web/**")
          .authenticated()
        .anyRequest()
          .fullyAuthenticated();

    http.formLogin()
          .loginPage("/login")
          .usernameParameter("userName")
          .passwordParameter("password")
          .failureUrl("/login?error")
          .defaultSuccessUrl("/web/")
          .permitAll()
        .and().logout()
          .logoutUrl("/logout")
          .logoutSuccessUrl("/")
          .permitAll()
        .and().rememberMe();
  }

我可以看到我的控制器正在被Spring加载,因为它们是使用http://localhost:8080/beans列出的,但我没有看到http://localhost:8080/mappings中的映射。

我的登录控制器非常简单。

@Controller
@Path("/login")
public class LoginWebController {

  @GET
  public ModelAndView getLoginPage(@RequestParam(required = false) String error) {
    return new ModelAndView("login", "error", error);
  }

}

谢谢,Wes。

1 个答案:

答案 0 :(得分:0)

我在解决问题后最终解决了这个问题,因此我可能已经做了几件事来解决问题,但我相信它就像删除对super.configure(http);的调用一样简单。这将设置基本身份验证。

韦斯。