Spring boot Instead of custom exception unauthorized is shown [closed]
up vote
0
down vote
favorite
I have a post endpoint to create a user. I want this endpoint to be security free.
I have achieved this in the WebSecurityConfiguration which extends WebSecurityConfigurerAdapter, with
web.ignoring().antMatchers(HttpMethod.POST, "/v1/user/create");
In my createUser method I have a check that if the username of a to-be-created user exists, then throws a UserAllreadyExistsException.
The problem is that if the username exists, instead of this exception I get the response below
{
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource"
}
Note: if I call this endpoint with a token, then I get the exception.
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/**", "/swagger-ui.html", "/webjars/**");
web.ignoring().antMatchers(HttpMethod.POST, "/v1/user/create");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/policies/language/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/privacy/language/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/packages/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/config/configName/**");
}}
public class OAuth2ResourceServerConfig extends esourceServerConfigurerAdapter {
@Autowired
private SigningKeyConfig signingKeyConfig;
@Override
public void configure(ResourceServerSecurityConfigurer config) {
config.tokenServices(tokenServices());
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(signingKeyConfig.getJwtSigningKey());
return converter;
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
return defaultTokenServices;
}
}
I thought of a workaround, instead of throwing the custom exception, i used the code below
if (userExists.isPresent()) {
log.error(UserUtil.USER_EXISTS);
JSONObject error = new JSONObject();
error.put("error", UserUtil.USER_EXISTS);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error.toString());
}
spring-boot spring-security
closed as off-topic by dur, snakecharmerb, sideshowbarker, Makyen, GhostCat Nov 20 at 7:52
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – dur, snakecharmerb, sideshowbarker, Makyen, GhostCat
If this question can be reworded to fit the rules in the help center, please edit the question.
add a comment |
up vote
0
down vote
favorite
I have a post endpoint to create a user. I want this endpoint to be security free.
I have achieved this in the WebSecurityConfiguration which extends WebSecurityConfigurerAdapter, with
web.ignoring().antMatchers(HttpMethod.POST, "/v1/user/create");
In my createUser method I have a check that if the username of a to-be-created user exists, then throws a UserAllreadyExistsException.
The problem is that if the username exists, instead of this exception I get the response below
{
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource"
}
Note: if I call this endpoint with a token, then I get the exception.
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/**", "/swagger-ui.html", "/webjars/**");
web.ignoring().antMatchers(HttpMethod.POST, "/v1/user/create");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/policies/language/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/privacy/language/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/packages/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/config/configName/**");
}}
public class OAuth2ResourceServerConfig extends esourceServerConfigurerAdapter {
@Autowired
private SigningKeyConfig signingKeyConfig;
@Override
public void configure(ResourceServerSecurityConfigurer config) {
config.tokenServices(tokenServices());
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(signingKeyConfig.getJwtSigningKey());
return converter;
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
return defaultTokenServices;
}
}
I thought of a workaround, instead of throwing the custom exception, i used the code below
if (userExists.isPresent()) {
log.error(UserUtil.USER_EXISTS);
JSONObject error = new JSONObject();
error.put("error", UserUtil.USER_EXISTS);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error.toString());
}
spring-boot spring-security
closed as off-topic by dur, snakecharmerb, sideshowbarker, Makyen, GhostCat Nov 20 at 7:52
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – dur, snakecharmerb, sideshowbarker, Makyen, GhostCat
If this question can be reworded to fit the rules in the help center, please edit the question.
Are you sure this only happens when the username doesn't exist? This is a typical Spring Security error message. Since those validations usually happen before any business logic is executed, I guess that this is entirely unrelated to theUserAlreadyExistsExceptionyou're throwing. You can verify that by trying a username that doesn't exist (should work), or by putting a breakpoint within thecreateUser()method.
– g00glen00b
Nov 19 at 9:46
It happens when i throw any exception inside this method. If i use a valid token then i get as a response the exception i throw.
– harrisKoud
Nov 19 at 10:23
I also test it with debug, it reaches the point the exception is thrown, but in the response i get the unauthorized. Not the exception.
– harrisKoud
Nov 19 at 10:56
Show your full Spring Security configuration.
– dur
Nov 19 at 19:36
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a post endpoint to create a user. I want this endpoint to be security free.
I have achieved this in the WebSecurityConfiguration which extends WebSecurityConfigurerAdapter, with
web.ignoring().antMatchers(HttpMethod.POST, "/v1/user/create");
In my createUser method I have a check that if the username of a to-be-created user exists, then throws a UserAllreadyExistsException.
The problem is that if the username exists, instead of this exception I get the response below
{
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource"
}
Note: if I call this endpoint with a token, then I get the exception.
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/**", "/swagger-ui.html", "/webjars/**");
web.ignoring().antMatchers(HttpMethod.POST, "/v1/user/create");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/policies/language/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/privacy/language/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/packages/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/config/configName/**");
}}
public class OAuth2ResourceServerConfig extends esourceServerConfigurerAdapter {
@Autowired
private SigningKeyConfig signingKeyConfig;
@Override
public void configure(ResourceServerSecurityConfigurer config) {
config.tokenServices(tokenServices());
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(signingKeyConfig.getJwtSigningKey());
return converter;
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
return defaultTokenServices;
}
}
I thought of a workaround, instead of throwing the custom exception, i used the code below
if (userExists.isPresent()) {
log.error(UserUtil.USER_EXISTS);
JSONObject error = new JSONObject();
error.put("error", UserUtil.USER_EXISTS);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error.toString());
}
spring-boot spring-security
I have a post endpoint to create a user. I want this endpoint to be security free.
I have achieved this in the WebSecurityConfiguration which extends WebSecurityConfigurerAdapter, with
web.ignoring().antMatchers(HttpMethod.POST, "/v1/user/create");
In my createUser method I have a check that if the username of a to-be-created user exists, then throws a UserAllreadyExistsException.
The problem is that if the username exists, instead of this exception I get the response below
{
"error": "unauthorized",
"error_description": "Full authentication is required to access this resource"
}
Note: if I call this endpoint with a token, then I get the exception.
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources/**", "/configuration/**", "/swagger-ui.html", "/webjars/**");
web.ignoring().antMatchers(HttpMethod.POST, "/v1/user/create");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/policies/language/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/privacy/language/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/user/packages/**");
web.ignoring().antMatchers(HttpMethod.GET, "/v1/config/configName/**");
}}
public class OAuth2ResourceServerConfig extends esourceServerConfigurerAdapter {
@Autowired
private SigningKeyConfig signingKeyConfig;
@Override
public void configure(ResourceServerSecurityConfigurer config) {
config.tokenServices(tokenServices());
}
@Bean
public TokenStore tokenStore() {
return new JwtTokenStore(accessTokenConverter());
}
@Bean
public JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
converter.setSigningKey(signingKeyConfig.getJwtSigningKey());
return converter;
}
@Bean
@Primary
public DefaultTokenServices tokenServices() {
DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
defaultTokenServices.setTokenStore(tokenStore());
return defaultTokenServices;
}
}
I thought of a workaround, instead of throwing the custom exception, i used the code below
if (userExists.isPresent()) {
log.error(UserUtil.USER_EXISTS);
JSONObject error = new JSONObject();
error.put("error", UserUtil.USER_EXISTS);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(error.toString());
}
spring-boot spring-security
spring-boot spring-security
edited Nov 21 at 21:24
asked Nov 19 at 9:17
harrisKoud
194
194
closed as off-topic by dur, snakecharmerb, sideshowbarker, Makyen, GhostCat Nov 20 at 7:52
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – dur, snakecharmerb, sideshowbarker, Makyen, GhostCat
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by dur, snakecharmerb, sideshowbarker, Makyen, GhostCat Nov 20 at 7:52
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example." – dur, snakecharmerb, sideshowbarker, Makyen, GhostCat
If this question can be reworded to fit the rules in the help center, please edit the question.
Are you sure this only happens when the username doesn't exist? This is a typical Spring Security error message. Since those validations usually happen before any business logic is executed, I guess that this is entirely unrelated to theUserAlreadyExistsExceptionyou're throwing. You can verify that by trying a username that doesn't exist (should work), or by putting a breakpoint within thecreateUser()method.
– g00glen00b
Nov 19 at 9:46
It happens when i throw any exception inside this method. If i use a valid token then i get as a response the exception i throw.
– harrisKoud
Nov 19 at 10:23
I also test it with debug, it reaches the point the exception is thrown, but in the response i get the unauthorized. Not the exception.
– harrisKoud
Nov 19 at 10:56
Show your full Spring Security configuration.
– dur
Nov 19 at 19:36
add a comment |
Are you sure this only happens when the username doesn't exist? This is a typical Spring Security error message. Since those validations usually happen before any business logic is executed, I guess that this is entirely unrelated to theUserAlreadyExistsExceptionyou're throwing. You can verify that by trying a username that doesn't exist (should work), or by putting a breakpoint within thecreateUser()method.
– g00glen00b
Nov 19 at 9:46
It happens when i throw any exception inside this method. If i use a valid token then i get as a response the exception i throw.
– harrisKoud
Nov 19 at 10:23
I also test it with debug, it reaches the point the exception is thrown, but in the response i get the unauthorized. Not the exception.
– harrisKoud
Nov 19 at 10:56
Show your full Spring Security configuration.
– dur
Nov 19 at 19:36
Are you sure this only happens when the username doesn't exist? This is a typical Spring Security error message. Since those validations usually happen before any business logic is executed, I guess that this is entirely unrelated to the
UserAlreadyExistsException you're throwing. You can verify that by trying a username that doesn't exist (should work), or by putting a breakpoint within the createUser() method.– g00glen00b
Nov 19 at 9:46
Are you sure this only happens when the username doesn't exist? This is a typical Spring Security error message. Since those validations usually happen before any business logic is executed, I guess that this is entirely unrelated to the
UserAlreadyExistsException you're throwing. You can verify that by trying a username that doesn't exist (should work), or by putting a breakpoint within the createUser() method.– g00glen00b
Nov 19 at 9:46
It happens when i throw any exception inside this method. If i use a valid token then i get as a response the exception i throw.
– harrisKoud
Nov 19 at 10:23
It happens when i throw any exception inside this method. If i use a valid token then i get as a response the exception i throw.
– harrisKoud
Nov 19 at 10:23
I also test it with debug, it reaches the point the exception is thrown, but in the response i get the unauthorized. Not the exception.
– harrisKoud
Nov 19 at 10:56
I also test it with debug, it reaches the point the exception is thrown, but in the response i get the unauthorized. Not the exception.
– harrisKoud
Nov 19 at 10:56
Show your full Spring Security configuration.
– dur
Nov 19 at 19:36
Show your full Spring Security configuration.
– dur
Nov 19 at 19:36
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Are you sure this only happens when the username doesn't exist? This is a typical Spring Security error message. Since those validations usually happen before any business logic is executed, I guess that this is entirely unrelated to the
UserAlreadyExistsExceptionyou're throwing. You can verify that by trying a username that doesn't exist (should work), or by putting a breakpoint within thecreateUser()method.– g00glen00b
Nov 19 at 9:46
It happens when i throw any exception inside this method. If i use a valid token then i get as a response the exception i throw.
– harrisKoud
Nov 19 at 10:23
I also test it with debug, it reaches the point the exception is thrown, but in the response i get the unauthorized. Not the exception.
– harrisKoud
Nov 19 at 10:56
Show your full Spring Security configuration.
– dur
Nov 19 at 19:36