Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Component
- public class JwtAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider {
- @Autowired
- private JwtValidator jwtValidator;
- @Override
- protected void additionalAuthenticationChecks(UserDetails userDetails, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
- }
- @Override
- protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
- JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken)authentication;
- String token = jwtAuthenticationToken.getToken();
- JwtUser jwtUser = jwtValidator.validate(token);
- if (jwtUser == null) {
- throw new RuntimeException("JWT Token not correct");
- }
- List<GrantedAuthority> grantedAuthorities = AuthorityUtils.commaSeparatedStringToAuthorityList(jwtUser.getRole());
- return new JwtUserDetails(jwtUser.getUserName(), jwtUser.getId(), grantedAuthorities, token);
- }
- @Override
- public boolean supports(Class<?> authentication) {
- return JwtAuthenticationToken.class.isAssignableFrom(authentication);
- }
- }
- public class JwtAuthenticationTokenFilter extends AbstractAuthenticationProcessingFilter {
- public JwtAuthenticationTokenFilter() {
- super("/rest/**");
- }
- @Override
- public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException {
- String header = request.getHeader("Authorization");
- if (header == null || !header.startsWith("Token ")) {
- throw new RuntimeException("JWT Token is missing");
- }
- String authenticationToken = header.substring(6);
- JwtAuthenticationToken token = new JwtAuthenticationToken(authenticationToken);
- return this.getAuthenticationManager().authenticate(token);
- }
- public void setAuthenticationManager(AuthenticationManager authenticationManager) {
- super.setAuthenticationManager(authenticationManager);
- }
- public void setAuthenticationSuccessHandler(JwtSuccessHandler jwtSuccessHandler) {
- }
- @Override
- protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authResult) throws IOException, ServletException {
- super.successfulAuthentication(request, response, chain, authResult);
- chain.doFilter(request, response);
- }
- }
- @Configuration
- @EnableWebSecurity
- @EnableGlobalMethodSecurity(prePostEnabled = true)
- public class JwtSecurityConfig extends WebSecurityConfigurerAdapter {
- @Autowired
- private JwtAuthenticationProvider authenticationProvider;
- @Autowired
- private JwtAuthenticationEntryPoint entryPoint;
- @Bean
- public AuthenticationManager authenticationManager() {
- return new ProviderManager(Collections.singletonList(authenticationProvider));
- }
- @Bean
- public JwtAuthenticationTokenFilter authenticationTokenFilter(){
- JwtAuthenticationTokenFilter filter = new JwtAuthenticationTokenFilter();
- filter.setAuthenticationManager(authenticationManager());
- filter.setAuthenticationSuccessHandler(new JwtSuccessHandler());
- return filter;
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- http.csrf().disable().authorizeRequests().antMatchers("/rest/**").authenticated().and().exceptionHandling().authenticationEntryPoint(entryPoint)
- .and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
- http.addFilterBefore(authenticationTokenFilter(), UsernamePasswordAuthenticationFilter.class);
- http.headers().cacheControl();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement