我正在尝试使用spring security来保护应用程序。通常我会实现自定义UserDeatilsService和CustomAuthenticationManager。但在这种情况下,UserDetailsService只有一个函数loadUserDeatils(String email)
。问题是我必须使用的类来检索用户的详细信息,没有一个函数只能通过用户名加载细节。它需要有两个参数,如用户名和密码。所以我回到只实现一个CustomAuthenticationManager并在那里自动化该类并在那里获取详细信息并检查它与身份验证,但我无法自动装配它,它显示为null。我怎样才能实现这一点,还是有更好的方法来做到这一点?我的安全配置
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/*@Autowired
MetadataGeneratorFilter metadataGeneratorFilter;
@Autowired
FilterChainProxy samlFilter;
@Autowired
SAMLEntryPoint samlEntryPoint;
*/
@Autowired
private CustomUserDetailsService customUserDetailsService;
/* @Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
System.out.println("metadataGeneratorFilter.toString() + samlEntryPoint.toString() + samlFilter.toString()+sdfsdf");
auth
.inMemoryAuthentication()
.withUser("user1")
.password("password1")
.roles("user")
.and()
.withUser("admin")
.password("password")
.roles("admin","user");
}*/
@Override
protected void configure(HttpSecurity http) throws Exception {
try {
http
.csrf().disable()
.authorizeRequests()
.antMatchers("/static/**").permitAll()
.antMatchers("/settings/api/**").permitAll()
.antMatchers("/api/**").permitAll()
.anyRequest().authenticated()
.and()
.formLogin()
.loginPage("/login").permitAll()
.loginProcessingUrl("/login")
// .usernameParameter("username").passwordParameter("password")
.defaultSuccessUrl("/index",true)
.and()
.httpBasic();
// .defaultSuccessUrl("/", "true);
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("sadhiasdniaaaaaaaaaaaaaaaa:");
e.printStackTrace();
}
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new ApiCustomAuthenticationProvider());
}
@Bean
public ApiCustomAuthenticationProvider apiCustomAuthenticationProvider() {
return new ApiCustomAuthenticationProvider();
}
@Bean
public SACSClientService sacsClientService(){
return new SACSClientService();
}
}我的CustomAuthenticationProvider
@Component
@Service
public class ApiCustomAuthenticationProvider implements AuthenticationProvider {
@Autowired
private SACSClientService sacsClientService;
@Autowired
private ReferralDaoImpl referralDaoImpl;
private ReferralProperty referralProperty =null ;
/*@PostConstruct
public void init()
{
ReferralProperty referralProperty = referralDaoImpl.getReferralProperty("sacs.service.base.url");
// sacsClientService.setWebServiceBaseUrl("http://10.41.92.43:11140");
sacsClientService.setWebServiceBaseUrl(referralProperty.getValue());
}*/
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
String username = authentication.getName();
String password = (String) authentication.getCredentials();
/* if(referralProperty==null)
referralProperty = referralDaoImpl.getReferralProperty("sacs.service.base.url");
// sacsClientService.setWebServiceBaseUrl("http://10.41.92.43:11140");
sacsClientService.setWebServiceBaseUrl(referralProperty.getValue());
if(sacsClientService == null )
System.out.println("***** Response 12" + "*****" );
if(referralDaoImpl ==null)
System.out.println("***** Response2134214234 " + "*****" );*/
// GetUserDetailsRequest request = new GetUserDetailsRequest(username, password);
System.out.println(username + password + "sadfkjgas" + "user.getName() " );
// AuthenticateAndGetUserRequest sacsRequest = new AuthenticateAndGetUserRequest(username,password);
// AuthenticateAndGetUserResponse sacsResponse = sacsClientService.authenticateAndGetUser(sacsRequest);
boolean loginStatus = false;
GetUserDetailsResponse userDetailsResponse = sacsClientService.authenticateAndGetUser(request);
UserSRO user = userDetailsResponse.getUser();*/
/* if (user != null) {
Set<AppRoleSRO> roles = user.getUserAppRoles();
List<GrantedAuthority> authorities = null;
if( roles !=null && !roles.isEmpty()) {
authorities = new ArrayList<GrantedAuthority>();
for (AppRoleSRO role : roles){
authorities.add(new SimpleGrantedAuthority(role.getAppRoleName()));
}
}
return new UsernamePasswordAuthenticationToken(username,password,authorities);
}else {
throw new BadCredentialsException("User does not exist on SACS");
}
}*/
List<GrantedAuthority> grantedAuths = new ArrayList<>();
grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER"));
return new UsernamePasswordAuthenticationToken("aman", "12345",grantedAuths);
}
@Override
public boolean supports(Class<?> authentication) {
return (UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication));
}
}`
SACSCliemtService是我无法自动装配的问题。一旦我使用它,它会给其中一个过滤器带来错误。当我注释掉SACSClient用法并传递虚拟值
时,代码运行良好