Я хочу использовать пользовательскую авторизацию в приложении весенней загрузки, где все запросы к авторизованному ресурсу имеют заголовок sessionId: uniqueSessionIdcharacters
. Какое место поставить логику, которая проверяет это? Я хочу:
- Иметь фильтр, который может обращаться к заголовкам и выполнять
select * from sessions
в моей собственной базе данных.
- Если этот сеанс существует, он вызывает следующий chain.filter и вводит пользователя в запрос, который я могу использовать в моем
@RestController
- Если его не существует, возвращается 401.
Сессия
@Entity
public class Session {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String sessionId;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id", nullable = false)
private User user;
}
WebSecurityConfigurerAdapter , пожалуйста, проверьте комментарии:
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider(new AuthenticationProvider() {
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
return null; // this thing isn't called at all;
}
@Override
public boolean supports(Class<?> aClass) {
return false; // this thing isn't called at all;
}
});
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.addFilterAfter(new Filter() {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
String session = httpServletRequest.getHeader("session_id");
// how do I tell spring that we're authorized here?
filterChain.doFilter(servletRequest, servletResponse);
}
@Override
public void destroy() {
}
}, BasicAuthenticationFilter.class)
.authorizeRequests()
.antMatchers("/login").permitAll()
.anyRequest().authenticated();
}
}
Я уже проверил много тем, но не могу найти полный пример.