Using a special Spring Security filter, I would like to return an HTTP 401 error code if the HTTP header does not contain a specific key-value pair.
Example:
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; final String val = request.getHeader(FOO_TOKEN) if(val == null || !val.equals("FOO")) {
As I understand it, I could do the following:
(1) ((HttpServletResponse) res).setStatus(401) and skip the rest of the filter chain
OR
(2) throws an exception that ultimately leads to Spring security throwing a 401 error to the client.
If # 1 is the best option, how can I skip the filter chain after calling setStatus(401) in the answer?
Or, if # 2 is the right way, what exception should I throw?
spring security
Kevin meredith
source share