Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AuthenticationFilter extends GenericFilterBean {
- @Override
- public void doFilter(ServletRequest request, ServletResponse response,
- FilterChain filterChain) throws IOException, ServletException{
- Authentication authentication = AuthenticationService
- .getAuthentication((HttpServletRequest)request);
- SecurityContextHolder.getContext()
- .setAuthentication(authentication);
- filterChain.doFilter(request, response);
- }
- }
- public class LoginFilter extends AbstractAuthenticationProcessingFilter {
- public LoginFilter(String url, AuthenticationManager authManager) {
- super(new AntPathRequestMatcher(url));
- setAuthenticationManager(authManager);
- }
- @Override
- public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
- throws AuthenticationException, IOException, ServletException {
- AccountCredentials creds = new ObjectMapper()
- .readValue(request.getInputStream(), AccountCredentials.class);
- return getAuthenticationManager().authenticate(
- new UsernamePasswordAuthenticationToken(
- creds.getUsername(),
- creds.getPassword(),
- Collections.emptyList()
- )
- );
- }
- @Override
- protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
- Authentication auth) throws IOException, ServletException {
- AuthenticationService.addToken(response, auth.getName());
- }
- }
- public class SecurityConfig extends WebSecurityConfigurerAdapter {
- @Autowired
- private UserDetailsServiceImpl userDetailsService;
- @Autowired
- public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception{
- auth.userDetailsService(userDetailsService)
- .passwordEncoder(new BCryptPasswordEncoder());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement