Welcome. Ask away! Unless otherwise specified we assume you're using the latest 5.x version of Spring Security
AuthenticationKeyGenerator
which can take into account some request param like device_id
. Then you can pass this generator to your tokenStore
and it will generate new access token based on different keys (returned from AuthenticationKeyGenerator.extractKey
). The uniqueness of this key determines if same or new access token will be used.
hello guys i am implementing user authentication in spring security i am stuck at when authentication exception occur at loginfilter
then method of LoginFilter.java
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
AuthenticationException failed) throws IOException, ServletException {
super.unsuccessfulAuthentication(request, response, failed);
ObjectMapper mapper = new ObjectMapper();
response.setCharacterEncoding("UTF-8");
response.getWriter().write(mapper.writeValueAsString(authException.getAuthentication().getInfo()));
}
gets executed in above method i have added Info
object in response.
now my question is that after LoginFilter
AuthFilter
(present in spring security configuration) is executed but only in error or any java exception
i don't want this to executed on exception because if user enter wrong user and pass
then i want to send response from LoginFilter
.
LoginFilter.java
public class LoginFilter extends AbstractAuthenticationProcessingFilter {
// constructor and beans;
@Override
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException, IOException, ServletException {
// other logic
return auth;
}
@Override
protected void successfulAuthentication(HttpServletRequest request, HttpServletResponse response, FilterChain chain,
Authentication authResult) throws IOException, ServletException {
// logic
}
@Override
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response,
AuthenticationException failed) throws IOException, ServletException {
super.unsuccessfulAuthentication(request, response, failed);
// this method is executed when user attempt wrong username or pass
ObjectMapper mapper = new ObjectMapper();
response.setCharacterEncoding("UTF-8");
response.getWriter().write(mapper.writeValueAsString(authException.getAuthentication().getInfo()));
}
AuthFilter.java
public class AuthFilter extends GenericFilterBean {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// some other logic
if (auth.isAuthenticated())
chain.doFilter(request, response);
else {
ObjectMapper mapper = new ObjectMapper();
res.getWriter().write(mapper.writeValueAsString(auth.getInfo()));
}
}
}
security config
.and()
.addFilterBefore(new LoginFilter(new AntPathRequestMatcher("/budget/login")),
UsernamePasswordAuthenticationFilter.class)
.addFilterBefore(new AuthFilter(),
UsernamePasswordAuthenticationFilter.class);
Hello Team...
I am trying to make a starter library (for reactive microservices) for my organization where in I want to configure only the /actuator/**
endpoints. I managed to isolate the configuration into an auto-configuration library and am defining the securityWebFilterChain
bean where i am able to use a securityMatcher
and perform the filtering...
Further down the chain, when one of the microservices using this library wants to add their own authentication scheme (e.g JWT checking) to the path, they are now having to re-define the full bean including the actuator config to get the required results.
Is there a built in way of allowing for this customization and injection of additional securityMatchers downstream if a library does the init upstream?
@GetMapping("/browse/{videoCatalogType}")
@PreAuthorize("hasAnyRole(\"" + AuthoritiesConstants.ADMIN + "," + AuthoritiesConstants.CHALCHITRA + "," + AuthoritiesConstants.SUBSCRIBED + "\")")
I'm trying to enable spring security only on the actuator endpoints, and allow all other requests, regardless of auth scheme to pass into the app code (which handles security in it's own regard).
Right now this is what I have:
@Bean
fun securityWebFilterChain(http: ServerHttpSecurity): SecurityWebFilterChain {
// NOTE: This will only add spring security to the actuator endpoints, all other endpoints will be handled
// by the JWT auth handling.
// https://stackoverflow.com/questions/38403740/authenticate-only-selected-rest-end-points-spring-boot
return http.authorizeExchange()
.pathMatchers("/actuator/**").authenticated()
.anyExchange().permitAll()
.and().httpBasic()
.and().csrf().disable()
.build()
}
@Bean
fun userDetailsService(): MapReactiveUserDetailsService {
return MapReactiveUserDetailsService(
User.withDefaultPasswordEncoder()
.username(actuatorAdminUsername)
.password(actuatorAdminPassword)
.roles("SUPERUSER")
.build()
)
}
This works to ensure that:
1) http basic auth can be used to access the actuator endpoints
2) any bearer request is passed through into the app code (which is what I want)
but doesn't allow an http basic auth request to pass through and be handled by my app code. I've tried a few different incantations to get it working as expected, but mostly get the current state or I get it to pass through http basic requests (at which case my auth on the actuator endpoints doesn't work). Any pointers?
@molexx How did you setup this? You need to create a custom AccessDeniedHandler
implementation and set it through HttpSecurity.exceptionHandling().accessDeniedHandler(myAccessDeniedHandler)
. Then you can implement handle
method as you wish.
For example:
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
response.setStatus(HttpStatus.FORBIDDEN.value());
response.getWriter().write(objectMapper.writeValueAsString(responseBody));
Or, in your case, you can set content-type to text/plain
and write a String.