Versiones comparadas

Clave

  • Se ha añadido esta línea.
  • Se ha eliminado esta línea.
  • El formato se ha cambiado.

...

Bloque de código
languagejava
themeEclipse
titleFiltro de autorización
linenumberstrue
public class JwtAuthorizationFilter extends OncePerRequestFilter {

	private static final org.apache.logging.log4j.Logger log = org.apache.logging.log4j.LogManager
			.getLogger( JwtAuthorizationFilter.class );

	@Autowired
	private ExpedientesService expedientesService;

	@Override
	protected void doFilterInternal( HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			FilterChain filterChain ) throws ServletException, IOException {

		if ( isPrivate( httpServletRequest ) ) {

			final String token = httpServletRequest.getHeader( HEADER_AUTHORIZATION_KEYHEADER );

			// En getSivaIdFromToken se obtienen los datos del token, lanzando las excepciones correspondientes 
			// si éste es incorrecto o ha expirado 
			final String sivaId = expedientesService.getSivaIdFromToken( token );
			// Si no hay sivaId o es -1, se lanza una excepción
			if ( ( sivaId == null ) || sivaId.contentEquals( "-1" ) ) {
				throw new UnauthorizedException();
			}
		}

		filterChain.doFilter( httpServletRequest, httpServletResponse );
	}

	private boolean isPrivate( HttpServletRequest httpServletRequest ) {
		final RequestMatcher privateUrl = new AntPathRequestMatcher( PRIVATE_PREFIX );
		return privateUrl.matches( httpServletRequest );
	}
}

...