用spring boot建的项目。
现在想自定义一个filter,要求实现用户名,密码,公司id一起验证。
下面是我的代码,参考UsernamePasswordAuthenticationFilter写的。
//这个是filter
public class UsernamePasswordSubdomainAuthenticationFilter extends AbstractAuthenticationProcessingFilter {
protected UsernamePasswordSubdomainAuthenticationFilter() {
super("/login");
}
@Override
public Authentication attemptAuthentication(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws AuthenticationException, IOException, ServletException {
String username = this.obtainUsername(httpServletRequest);
String password = this.obtainPassword(httpServletRequest);
String subdomain = this.obtainSubDomain(httpServletRequest);
if(username == null) {
username = "";
}
if(password == null) {
password = "";
}
if(subdomain == null){
subdomain = "";
}
username = username.trim();
UsernamePasswordSubdomainAuthenticationToken authRequest = new UsernamePasswordSubdomainAuthenticationToken(username, password, subdomain);
this.setDetails(httpServletRequest, authRequest);
return this.getAuthenticationManager().authenticate(authRequest);
}
protected void setDetails(HttpServletRequest request, UsernamePasswordSubdomainAuthenticationToken authRequest) {
authRequest.setDetails(this.authenticationDetailsSource.buildDetails(request));
}
public String obtainUsername(HttpServletRequest request) {
return request.getParameter("username");
}
public String obtainPassword(HttpServletRequest request) {
return request.getParameter("password");
}
public String obtainSubDomain(HttpServletRequest request) throws MalformedURLException {
URL url = new URL(request.getRequestURL().toString());
String subDomain = url.getHost().split("\\.")[0];
return subDomain;
}
}
//这个是配置
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
// @Autowired
// private UsernamePasswordSubdomainAuthenticationFilter usernamePasswordSubdomainAuthenticationFilter;
@Bean
public UsernamePasswordSubdomainAuthenticationFilter usernamePasswordSubdomainAuthenticationFilter() {
System.out.println(this.authenticationManager);
UsernamePasswordSubdomainAuthenticationFilter filer = new UsernamePasswordSubdomainAuthenticationFilter();
filer.setAuthenticationManager(authenticationManager);
return filer;
}
// @Autowired
private AuthenticationManager authenticationManager;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user") // #1
.password("pass")
.roles("USER")
.and()
.withUser("admin") // #2
.password("password")
.roles("ADMIN","USER");
}
@Override
public void configure(WebSecurity web) throws Exception {
// web
// .ignoring()
// .antMatchers("/resources/**"); // #3
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.addFilter(usernamePasswordSubdomainAuthenticationFilter())
.formLogin().disable()
.httpBasic().disable()
.csrf()
.disable();
}
}
然后报了下面的错误
Caused by: java.lang.IllegalArgumentException: authenticationManager must be specified
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.afterPropertiesSet(AbstractAuthenticationProcessingFilter.java:164)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
黄舟2017-04-17 15:33:30
개인적으로 springSecurity를 사용하고 싶지만 전체 인증 과정의 소스 코드를 완전히 이해하지 못했다면 사용하지 않는 것이 가장 좋다고 생각합니다. 그렇지 않으면 프로젝트가 완전히 실패하게 됩니다.
소스 코드를 더 자세히 살펴보면 문제 오류 메시지에서 인증 관리자가 정의되지 않았음을 분명히 알 수 있습니다. 주의 깊게 읽을 기사 추천
http://www.liaozhida.net/springsecurity/springsecurity-简单拦截验证uml图.html
SPRINGSECURITY 소스 코드 분석 – 사용자 로그인 프로세스 중에 발생하는 일