【发布时间】: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 中使用,而不是登录验证所需的 CustomUserDetailsService。 我该如何改变?
如果需要:
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 但即使有您的建议,它仍然不会触发 CustomUserDetailsService 中的 loadUserByUsername 方法。
-
您正在使用 JAX-RS,因此请确保正确设置了集成以及您正在调用的服务的 URL 是什么。您可能还想将
@Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)添加到您的配置类中。 -
我正在使用 spring-boot-starter-jersey 包,没有其他配置。 URL 是 localhost:8888/api/user/authenticate 并且它仍然不想点击 CustomUserDetailsService...
-
你的依赖列表中有哪些依赖(starters)。
标签: spring security spring-mvc spring-security spring-boot