gpt4 book ai didi

java - 使用 UserDetailsS​​ervice 和 Java 配置的 Spring Security 登录

转载 作者:行者123 更新时间:2023-11-30 06:14:05 25 4
gpt4 key购买 nike

我正在尝试通过 Spring Security 的数据库查找来添加登录功能,但我真的很挣扎。我想通过实现 UserDetailsS​​ervice 接口(interface)来查找用户。我已经阅读了大量文档并在 Google 上搜索了几个小时,但我仍然被困住了。我能找到的大多数示例都使用 XML,所以我的问题可能与将这些示例转换为 Java 配置有关。

UserDetailsS​​ervice

@Service
public class AccountServiceImpl implements AccountService { // AccountService implements UserDetailsService
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
// The email variable is always ''

List<GrantedAuthority> authList = new ArrayList<>();
authList.add(new Role("ROLE_USER")); // Role implements GrantedAuthority

return new User("test@example.com", "password", true, true, true, true, authList);
}
}

安全配置

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private LogoutSuccessHandler logoutSuccessHandler;

@Autowired
private AccountService accountService;


@Override
@Bean
protected AuthenticationManager authenticationManager() throws Exception {
// This bean is required for using method security annotations
return super.authenticationManager();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.formLogin()
.loginPage("/login")
.loginProcessingUrl("/login/process")
.failureUrl("/login?error=true")
.defaultSuccessUrl("/", false)
.and()
//.userDetailsService(this.accountService)
.logout()
.logoutUrl("/logout")
.logoutSuccessHandler(this.logoutSuccessHandler)
.invalidateHttpSession(true)
.and()
// Permissions here
.csrf().disable();
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("test@example.com").password("password").roles("USER");
}

/*@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
//.userDetailsService(this.accountService);
.inMemoryAuthentication()
.withUser("test@example.com").password("password").roles("USER").and()
.withUser("admin").password("password").roles("USER", "ADMIN");
}*/

/*@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setUserDetailsService(this.accountService);
return provider;
}*/
}

如您所见,有一堆注释掉的代码,它们只是我试图让它工作的一些东西。我还尝试了简单的内存中身份验证,但这也没有用。

登录.jsp

<form action="/login/process" method="POST">
<input name="j_username" id="j_username" type="text" />
<input name="j_password" id="j_password" type="password" />

<input type="submit" value="Login" />
</form>

调试

DEBUG org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - Request is to process authentication

DEBUG org.springframework.security.authentication.ProviderManager - Authentication attempt using org.springframework.security.authentication.dao.DaoAuthenticationProvider

DEBUG org.springframework.security.authentication.dao.DaoAuthenticationProvider - User '' not found

DEBUG org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - Authentication request failed: org.springframework.security.authentication.BadCredentialsException: Bad credentials

DEBUG org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter - Updated SecurityContextHolder to contain null Authentication

以上似乎总是适用于我尝试过的所有配置。我什至不确定为什么使用 DaoAuthenticationProvider,即使我试图覆盖它;我想这是一个默认值。对于上面的一些配置,我的 UserDetailsS​​ervice 实现被调用,但使用一个空字符串作为参数。此外,即使我返回一个硬编码的 UserDetails 对象,其凭据与我在表单中输入的凭据相同,身份验证仍然失败,并且我被重定向回 /login?error=是的

我在这里做错了什么?请告知我的错误或提供一个使用 Java 配置的简单示例。谢谢!

最佳答案

在基于 Java 的配置中使用 Spring Security 时,请求参数的名称是 usernamepassword 而不是 j_usernamej_password 了。

您可以修改您的登录表单

<form action="/login/process" method="POST">
<input name="username" id="j_username" type="text" />
<input name="password" id="j_password" type="password" />

<input type="submit" value="Login" />
</form>

或调整您的配置以使用旧的字段名称。

formLogin().usernameParameter("j_username").passwordParameter("j_password");

无论哪种方式都可以解决您的问题。

关于java - 使用 UserDetailsS​​ervice 和 Java 配置的 Spring Security 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30892187/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com