【问题标题】:Spring Security Basic Auth not using CustomUserDetailsServiceSpring Security Basic Auth 不使用 CustomUserDetailsS​​ervice
【发布时间】:2015-03-10 10:13:37
【问题描述】:

我的项目在最新的 Spring Boot + Jersey 中,但登录验证有问题。

我的安全配置是:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
@Autowired
private DataSource datasource;

@Override
protected void configure(HttpSecurity http) throws Exception
{
    http.csrf().disable().authorizeRequests()
            .antMatchers("/api/user/**").permitAll()
            .antMatchers("/api/**").authenticated().and().httpBasic();

}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
    auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
    auth.jdbcAuthentication().dataSource(datasource);
}

@Bean
public UserDetailsService userDetailsService()
{
    return new CustomUserDetailsService(datasource);
}
}

但是当我这样做时:

@POST
@Path("authenticate")
@Produces(MediaType.APPLICATION_JSON)
public Response authenticate(@HeaderParam("username") String username, @HeaderParam("password") String password)
{
    UsernamePasswordAuthenticationToken authenticationToken =
            new UsernamePasswordAuthenticationToken(username, password);
    Authentication authentication = this.authManager.authenticate(authenticationToken);
    SecurityContextHolder.getContext().setAuthentication(authentication);

    UserDetails userDetails = this.userService.loadUserByUsername(username);

    return createOkResponse(userDetails.getUsername());
}

authenticate 方法在 InMemoryUserDetailsManager 中使用,而不是登录验证所需的 CustomUserDetailsS​​ervice。 我该如何改变?

如果需要:

public class CustomUserDetailsService extends JdbcUserDetailsManager implements UserDetailsService
{
@Autowired
private UserRepository userRepository;

public CustomUserDetailsService(DataSource datasource)
{
    setDataSource(datasource);
}

@Override
public CurrentUserInfo loadUserByUsername(String email)
        throws UsernameNotFoundException
{

    User user = userRepository.findByPrimaryEmailAndEnabledTrue(email);
    handleUserNotFound(email, user);

    return new CurrentUserInfo(user);
}

private void handleUserNotFound(String email, User user)
{
    if (user == null)
    {
        throw new UsernameNotFoundException("No user found with email: " + email);
    }
}
}

启动器依赖项:

compile("org.springframework.boot:spring-boot-starter-web") {
    exclude module: 'spring-boot-starter-tomcat'
}
compile "org.springframework.boot:spring-boot-starter-jetty"
compile "org.springframework.boot:spring-boot-starter-security"
compile "org.springframework.boot:spring-boot-starter-aop"
compile "org.springframework.boot:spring-boot-starter-data-jpa"
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.boot:spring-boot-starter-jersey"
compile("org.springframework.boot:spring-boot-starter-actuator") { exclude module: 'hsqldb' }

【问题讨论】:

  • 因为你没有使用它。 auth.jdbcAuthentication().dataSource(datasource); 行有效地禁用了上面的行,使您的配置无用。此外,如果您使用的是 Spring Boot,您可以删除 @EnableWebSecurity,因为 Spring Boot 已经完成了。
  • Thx Deinum 但即使有您的建议,它仍然不会触发 CustomUserDetailsS​​ervice 中的 loadUserByUsername 方法。
  • 您正在使用 JAX-RS,因此请确保正确设置了集成以及您正在调用的服务的 URL 是什么。您可能还想将@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) 添加到您的配置类中。
  • 我正在使用 spring-boot-starter-jersey 包,没有其他配置。 URL 是 localhost:8888/api/user/authenticate 并且它仍然不想点击 CustomUserDetailsS​​ervice...
  • 你的依赖列表中有哪些依赖(starters)。

标签: spring security spring-mvc spring-security spring-boot


【解决方案1】:

问题是您正在“注册”一个仅可用于/api URI 的AuthenticationManager,并且它没有作为一个bean 暴露给ApplicationContext。当使用 Spring Boot 时,它将添加一个全局用户并在其中注入一个带有生成密码的默认用户。

Spring Boot 自动配置将检测是否存在已经可用的 AuthenticationManager。要注册一个全局变量,只需创建一个方法,该方法将AuthenticationManagerBuilder 作为参数并使用@Autowired 进行注释。只需确保它没有被命名为 configure,因为这不起作用。

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
    auth.jdbcAuthentication().dataSource(datasource);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-21
    • 2017-06-14
    • 2012-06-11
    • 1970-01-01
    • 2016-10-02
    • 2020-11-09
    • 1970-01-01
    • 2013-12-19
    相关资源
    最近更新 更多