我正在尝试设置一个基于REST的Web应用程序,其中前端使用Reactjs,后端使用Sping Boot 。我还尝试设置一个自定义身份验证提供程序,这就是我的问题开始的地方。当尝试测试登录API调用时,CustomAuthenticationProvider从未被调用,而是使用默认的DaoAuthenticationProvider。这导致登录报告“凭据错误”。
我已经上传了一个小示例应用程序到github:spring-boot-auth-demo
为了测试登录API,我使用了以下curl:
curl -H "Content-Type: application/json" -X POST -d '{"username":"admin","password":"admin"}' http://localhost:8080/api/users/login
CustomAuthenticationProvider会执行简单的使用者名称/密码检查,并传回UsernamePasswordAuthenticationToken对象。
package no.bluebit.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class CustomAuthenticationProvider implements AuthenticationProvider {
private static final Logger logger = LoggerFactory.getLogger(CustomAuthenticationProvider.class);
public CustomAuthenticationProvider() {
logger.info("***CustomAuthenticationProvider created");
}
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
if(authentication.getName().equals("admin") && authentication.getCredentials().equals("admin")) {
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
grantedAuths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
return new UsernamePasswordAuthenticationToken(authentication.getName(), authentication.getCredentials(), grantedAuths);
} else {
return null;
}
}
@Override
public boolean supports(Class<?> authentication) {
return UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication);
}
}
CustomAuthenticationProvider是使用SecurityConfiguration类连接的。在单步执行代码时,我可以看到CustomAuthenticationProvider不在用于对传入请求进行身份验证的提供程序列表中。
package no.bluebit.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.authenticationProvider(this.customAuthenticationProvider);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/api/users/login").permitAll() // Permit access for all to login REST service
.antMatchers("/").permitAll() // Neccessary to permit access to default document
.anyRequest().authenticated().and() // All other requests require authentication
.httpBasic().and()
.logout().and()
.csrf().disable();
}
}
为什么这不管用?
3条答案
按热度按时间3htmauhk1#
尝试在标头http上添加this thought:
示例:
我做了这个应用程序与Spring安全和棱角!前面:https://github.com/nicobassart/angularforHidra背面:https://github.com/nicobassart/hidra_server
qncylg1j2#
查看AuthenticationProvider类(分别是java文档)
方法authenticate预期:
如果返回null,则将调用下一个AuthenticationProvider,这是默认值。
我不确定这是不是问题所在,但这可能是个问题。尝试抛出BadCredentialsException,就像AuthenticationManager类告诉你的那样:
zmeyuzjn3#
你必须以其他方式设置凭证。试着看一个用户名密码令牌的工作示例。但是你的“authenticate”函数需要是设置凭证的函数。