【发布时间】:2019-04-29 09:21:42
【问题描述】:
我正在尝试将自定义过滤器添加到 Spring HttpSecurity。此过滤器必须检查用户名是否在外部提供的列表中,并以Set 的形式注入过滤器。
无论我将过滤器放在哪里,它的方法attemptAuthentication 都不会被调用。这是过滤器代码:
import java.io.IOException;
import java.util.Base64;
import java.util.Set;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
public class MyRoleFilter extends AbstractAuthenticationProcessingFilter {
final Set<String> authorisedUsers;
public WhoRoleFilter(String url, AuthenticationManager authenticationManager, Set<String> authorisedUsers) {
super(new AntPathRequestMatcher(url));
this.authorisedUsers= authorisedUsers;
}
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException, IOException, ServletException {
// In BASIC authentication user:password come as Base64 in the Authorization header
final String authorization = request.getHeader("Authorization");
final String[] userPasswd = new String(Base64.getDecoder().decode(authorization)).split(":");
// The docs of AbstractAuthenticationProcessingFilter says it must throw an exception in case authentication fails
// https://docs.spring.io/spring-security/site/docs/4.2.6.RELEASE/apidocs/org/springframework/security/web/authentication/AbstractAuthenticationProcessingFilter.html#attemptAuthentication-javax.servlet.http.HttpServletRequest-javax.servlet.http.HttpServletResponse-
if (userPasswd.length!=2)
throw new BadCredentialsException("Bad Credentials");
if (authorisedUsers.contains(userPasswd[0])) {
UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(userPasswd[0], userPasswd[1]);
return this.getAuthenticationManager().authenticate(authRequest);
} else {
throw new BadCredentialsException("User has not the correct role");
}
}
}
这就是我尝试将其添加到 HttpSecurity 的方式:
@EnableWebSecurity
@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) {
http.csrf().disable()
.authorizeRequests()
.antMatchers("/disabled")
.permitAll()
.anyRequest()
.authenticated()
.and()
.addFilterBefore(new MyRoleFilter("**/path/services/whatever/**", this.authenticationManager() ,myUserNamesSet), BasicAuthenticationFilter.class)
.httpBasic();
}
}
我不确定在构建链中addFilterBefore() 必须去哪里。此外,除了用户名列表过滤器之外,还需要 LDAP 服务器的标准用户+密码。 LDAP 身份验证已经到位并处于正常工作状态。
更新,这是configureGlobal(AuthenticationManagerBuilder)SecurityConfig
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
try {
auth.ldapAuthentication()
.userDnPatterns(ConfigurationProvider.get().getProperty(Property.PROP_LDAP_USER_BASE_DN))
.contextSource(contextSource())
.passwordCompare()
.passwordAttribute(ConfigurationProvider.get().getProperty(Property.PROP_LDAP_PASSWORD_ATTRIBUTE));
} catch (Exception exc) {
LOG.error(exc.getMessage(), exc);
}
}
【问题讨论】:
标签: spring-security