Title: Spring Boot and Spring Security, can't send error with custom message in AuthenticationEntryPoint
Tags: java;spring;spring-boot
Question: I'm working on a restful api using spring boot, and going to use a custom authenticationEntryPoint. Here is the code
```@Component
public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {

    private static final Logger logger = LoggerFactory.getLogger(JwtAuthenticationEntryPoint.class);
    @Override
    public void commence(HttpServletRequest httpServletRequest,
                         HttpServletResponse httpServletResponse,
                         AuthenticationException e) throws IOException, ServletException {
        logger.error(&quot;Responding with unauthorized error. Message - {}&quot;, e.getMessage());
        httpServletResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
    }
}
```
And the related part in Security configuration.
```@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(
        securedEnabled = true,
        jsr250Enabled = true,
        prePostEnabled = true
)
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
   ...

    @Autowired
    private JwtAuthenticationEntryPoint unauthorizedHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors().and().csrf().disable()
                .exceptionHandling()
                    .authenticationEntryPoint(unauthorizedHandler)
                    .and()
                .sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                    .and()
                .authorizeRequests()
                    .antMatchers(HttpMethod.GET, &quot;/api/**&quot;).permitAll()
                    .antMatchers(HttpMethod.POST, &quot;/api/auth/**&quot;).permitAll()
                    .antMatchers(HttpMethod.GET, &quot;/api/users/checkUsernameAvailability&quot;, &quot;/api/users/checkEmailAvailability&quot;).permitAll()
                .anyRequest().authenticated();

        http.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);
    }
    ...
}

```
The problem is, when I log in with a wrong credential, the error message is empty.
```{
    &quot;timestamp&quot;: &quot;2020-09-03T17:16:28.082+00:00&quot;,
    &quot;status&quot;: 401,
    &quot;error&quot;: &quot;Unauthorized&quot;,
    &quot;message&quot;: &quot;&quot;,
    &quot;path&quot;: &quot;/api/auth/signin&quot;
}
```
However, the logger prints correct message
```c.m.p.s.JwtAuthenticationEntryPoint      : Responding with unauthorized error. Message - Bad credentials
```
What may go wrong here? Thank you in advance.


Here is the accepted answer: Default value for ```server.error.include-message``` is ```&quot;never&quot;```.
Therefore, you just need to configure your ```application.yaml``` (or properties) like this
```server:
  error:
    include-message: always
    include-binding-errors: always
```

Comment for this answer: A follow up question, I see other spring projects don't call this out but the error message goes along. How come they are able to send error message without configuring?
Comment for this answer: @MillerDong It depends which version of spring boot is used in project. Since Spring Boot 2.3 they changed default value of `server.error.include-message` to `never`


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:09/03/20', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<java><spring><spring-boot>', 'Title': "Spring Boot and Spring Security, can't send error with custom message in AuthenticationEntryPoint", 'ViewCount': 1400, 'id': 63730436, 'user_AboutMe': '', 'user_AccountId': 14457971.0, 'user_DisplayName': 'Miller Dong', 'user_Reputation': 79.0, 'user_Views': 8.0, 'user_id': 10443611.0}}