gpt4 book ai didi

java - 来自数据库的 Spring Boot Security REST 基本身份验证

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:49:30 26 4
gpt4 key购买 nike

我遇到一个问题,当我使用 inMemoryAuthentication 进行基本身份验证时,如以下代码片段所示,它运行良好。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
DataSource dataSource;

@Autowired
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1").password("secret1").roles("USER")
.and()
.withUser("admin").password("123456").roles("ADMIN");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().authorizeRequests().antMatchers("/**").hasRole("ADMIN").and()
.csrf().disable().headers().frameOptions().disable();
}

}

但是当我尝试使用 get database 来获取将用于身份验证的用户数据时,它不起作用,只会发回 403 响应。

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
DataSource dataSource;//MySQL db via JPA

@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
auth.jdbcAuthentication().dataSource(dataSource).usersByUsernameQuery("select username, password, 1 as enabled from user where username=?")
.authoritiesByUsernameQuery("select username, role from user where username=?");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().authorizeRequests().antMatchers("/**").hasRole("ADMIN").and()
.csrf().disable().headers().frameOptions().disable();

}

}

现在,只有 Spring-Boot REST 应用程序才会出现这种情况,我在 Spring MVC 应用程序和/login 页面中尝试了相同的方法,它适用于 inMemoryAuthentication 和 < strong>jdbcAuthentication.

最佳答案

我遇到了同样的问题,就我而言,以下解决方案非常有效

创建一个实现AuthenticationProvider接口(interface)的类:

@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {

@Autowired
private UserService userService;

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {

String email = authentication.getName();
String password = authentication.getCredentials().toString();

User user = userService.findUserByEmail(email);

List<GrantedAuthority> authorities = new ArrayList<>();
authorities.add(new SimpleGrantedAuthority(user.getRole().getDescription())); // description is a string

return new UsernamePasswordAuthenticationToken(email, password, authorities);
}

@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}

这里要对用户进行身份验证,您使用用户服务通过电子邮件(用户名)从数据库中检索用户,并使用他的电子邮件、密码和他授予的权限(例如:USER、ADMIN)创建 token

然后在您的 SecurityConfig 类中使用您刚刚创建的 bean,如下所示:

@Configuration
@EnableGlobalMethodSecurity(securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private CustomAuthenticationProvider authProvider;

@Autowired
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(authProvider);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic().and().authorizeRequests().antMatchers("/**").hasRole("ADMIN").and()
.csrf().disable().headers().frameOptions().disable();
}
}

关于java - 来自数据库的 Spring Boot Security REST 基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46124028/

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