• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

JWT w Spring Boot trudnosci w implementacji

VPS Starter Arubacloud
+1 głos
236 wizyt
pytanie zadane 12 grudnia 2019 w Java przez JuniorPL Użytkownik (770 p.)

Cześć mam trudnośći w implementacji JWT. Stworzyłem filtr:

 

public class AuthenticationFilter extends UsernamePasswordAuthenticationFilter
{
private final AuthenticationManager authenticationManager;

public AuthenticationFilter(AuthenticationManager authenticationManager) 
{	
	this.authenticationManager = authenticationManager;
}

@Override
    public Authentication attemptAuthentication(HttpServletRequest request,
			HttpServletResponse response) throws AuthenticationException {

	try {
		UserLoginRequestModel creds = new ObjectMapper().readValue(request.getInputStream(),UserLoginRequestModel.class);

		UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken = new UsernamePasswordAuthenticationToken(
				creds.getEmail(),
				creds.getPassword(),
				new ArrayList<>());

		authenticationManager.authenticate(usernamePasswordAuthenticationToken);

		return authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(
				creds.getEmail(),
				creds.getPassword(),
				new ArrayList<>()));
	}
catch(IOException e)
	{
	throw new RuntimeException(e);
	}
    
    }


@Override
protected void successfulAuthentication(HttpServletRequest request,
        HttpServletResponse res,FilterChain chain, Authentication auth) throws IOException, ServletException
{
	String userName = ((User)auth.getPrincipal()).getUsername();
	
	String token = Jwts.builder()
			.setSubject(userName)
			//.claim("scope","ADMIN")
			.setExpiration(new Date(System.currentTimeMillis() + SecurityConstants.EXPIRATION_TIME))
			.signWith(SignatureAlgorithm.HS512, SecurityConstants.getTokenSecret())
			.compact();
	
	UserService userService = (UserService) SpringApplicationContext.getBean("userServiceImpl");
	UserDto userDto = userService.getUser(userName);
	
	res.addHeader(SecurityConstants.HEADER_STRING,SecurityConstants.TOKEN_PREFIX+token);
	
	res.addHeader("UserID",userDto.getUserId());
	
}
}
public class AuthorizationFilter extends BasicAuthenticationFilter 
{

	public AuthorizationFilter(AuthenticationManager authManager)
	{
		super(authManager);
	}
	
	
	
	@Override
	protected void doFilterInternal(HttpServletRequest req,
	        HttpServletResponse res,FilterChain chain) throws IOException, ServletException
	{
		String header = req.getHeader(SecurityConstants.HEADER_STRING);
		
	if(header== null || !header.startsWith(SecurityConstants.TOKEN_PREFIX))
			{
		chain.doFilter(req, res);
			return;
			}
		
		UsernamePasswordAuthenticationToken authentication = getAuthentication(req);
		SecurityContextHolder.getContext().setAuthentication(authentication);
		chain.doFilter(req, res);
	
	}



	private UsernamePasswordAuthenticationToken getAuthentication(HttpServletRequest req)
	{
	String token = req.getHeader(SecurityConstants.HEADER_STRING);
	
	if(token != null)
	{
		token = token.replace(SecurityConstants.TOKEN_PREFIX,"");
	
	String user = Jwts.parser()
			.setSigningKey(SecurityConstants.getTokenSecret())
			.parseClaimsJws(token)
			.getBody()
			.getSubject();
			
	if(user != null)
	{
		List<SimpleGrantedAuthority> authorities = new ArrayList<>();
		authorities.add(new SimpleGrantedAuthority("ADMIN"));
		return new UsernamePasswordAuthenticationToken(user, null,authorities );
	}
	
	}
		return null;
	}
	
}

Oraz config:


@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
    private final UserService userDetailsService;
    private final BCryptPasswordEncoder bCryptPasswordEncoder;

    public WebSecurity(UserService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
        super();
        this.userDetailsService = userDetailsService;
        this.bCryptPasswordEncoder = bCryptPasswordEncoder;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
    }


    @Override
    protected void configure(HttpSecurity http) throws Exception {
		http.csrf().disable().authorizeRequests().antMatchers(HttpMethod.POST, SecurityConstants.SIGN_UP_URL)
				.permitAll()
				//.antMatchers(HttpMethod.GET,"/users/showUser/**").hasRole("ADMIN")
				.anyRequest()
				.authenticated()
				.and()
				.addFilter(getAuthenticationFilter())
				.addFilter(new AuthorizationFilter(authenticationManager()))
				.sessionManagement()
				.sessionCreationPolicy(SessionCreationPolicy.STATELESS);


	}

    public AuthenticationFilter getAuthenticationFilter() throws Exception {
        final AuthenticationFilter filter = new AuthenticationFilter(authenticationManager());
        filter.setFilterProcessesUrl("/users/login");
        return filter;
    }

}

Rola jest wpisana hardcodem ale tylko na potrzebę testów. Problem polega na tym że nie do końca wiem czy rola jest zapisywana w tokenie. Gdy chcę ograniczać dostęp w oparciu o rolę dostaję brak dostępu. Może ktoś rzuci okiem i coś podpowie.

 

Zaloguj lub zarejestruj się, aby odpowiedzieć na to pytanie.

Podobne pytania

0 głosów
2 odpowiedzi 1,415 wizyt
pytanie zadane 10 sierpnia 2019 w Java przez Aisekai Nałogowiec (42,190 p.)
+1 głos
0 odpowiedzi 108 wizyt
0 głosów
1 odpowiedź 65 wizyt

92,453 zapytań

141,262 odpowiedzi

319,088 komentarzy

61,854 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Akademia Sekuraka 2024 zapewnia dostęp do minimum 15 szkoleń online z bezpieczeństwa IT oraz dostęp także do materiałów z edycji Sekurak Academy z roku 2023!

Przy zakupie możecie skorzystać z kodu: pasja-akademia - użyjcie go w koszyku, a uzyskacie rabat -30% na bilety w wersji "Standard"! Więcej informacji na temat akademii 2024 znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...