Spring Boot Security / Request = 500

Hello guys, I'm a beginner here who's trying Spring Boot security. I'm trying to sign in with the httpSecurity but when I do my sign-in request, SPring boot redirects me to the famous
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jul 20 23:12:55 CEST 2024
There was an unexpected error (type=Internal Server Error, status=500).
Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Sat Jul 20 23:12:55 CEST 2024
There was an unexpected error (type=Internal Server Error, status=500).
issue. The thing is in my security configuration I understand I have to modify this line :
.formLogin(form -> form

// Redirect the user if successfull authentication
.defaultSuccessUrl("/",true)

)
.logout(config-> config.logoutSuccessUrl("/"))
// Redirect the user if log the fuck out
.build();
.formLogin(form -> form

// Redirect the user if successfull authentication
.defaultSuccessUrl("/",true)

)
.logout(config-> config.logoutSuccessUrl("/"))
// Redirect the user if log the fuck out
.build();
But my home URL is http://localhost:8080/ do you know how to modify the url to at least redirect it to the homepage ( I built my environment with Maven ) ?
61 Replies
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @NoSung! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st5mo ago
Can you show the stack trace?
Sung
SungOP5mo ago
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "$(_csrf.parameterName)" (template: "index" - line 39, col 42)
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "$(_csrf.parameterName)" (template: "index" - line 39, col 42)
Basically an exception and this is my code at line 39 in my index.html :
<form action="/logout" method="post" sec:authorize="isAuthenticated()">
<input type="hidden" th:name="$(_csrf.parameterName)"
th:value="${_csrf.token}" />
<form action="/logout" method="post" sec:authorize="isAuthenticated()">
<input type="hidden" th:name="$(_csrf.parameterName)"
th:value="${_csrf.token}" />
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
dan1st
dan1st5mo ago
Can you show the full stack trace instead of a part of it? Also for the security configuration, did you show the complete security configuration or is there more to it?
Peter Rader
Peter Rader5mo ago
Solution: Use curly bracets instead of round bracets.
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Sung
SungOP5mo ago
Sorry for that. @Peter Rader found the solution, I did a stupid syntax error as he said
dan1st
dan1st5mo ago
yeah I saw that ;) maybe I should increase my font size lol
Sung
SungOP5mo ago
Do you know if there's a way to personalize the errorMessage function ?
dan1st
dan1st5mo ago
which function exactly?
Sung
SungOP5mo ago
I think I should do that too, because I wasn't even aware of the slack track, I was pretty sure I had to configure my security config and not my static file lol
dan1st
dan1st5mo ago
it isn't a static file it's a template
Sung
SungOP5mo ago
Here in my Account controller :
@PostMapping("/register")
public String register(
Model model,
@Valid @ModelAttribute RegisterDto registerDto,
BindingResult result
){
// Handling authentification errors if its equals to the Password field and Confirm password are different
if(!registerDto.getPassword().equals(registerDto.getConfirmPassword())){
result.addError(
new FieldError("registerDto", "confirmPassword"
, "Password and confirm password do not match")
);
}

AppUser appUser = repo.findByEmail(registerDto.getEmail());
if(appUser != null){
result.addError(
new FieldError("registerDto","email"
,"Email adress is already used")
);
}
@PostMapping("/register")
public String register(
Model model,
@Valid @ModelAttribute RegisterDto registerDto,
BindingResult result
){
// Handling authentification errors if its equals to the Password field and Confirm password are different
if(!registerDto.getPassword().equals(registerDto.getConfirmPassword())){
result.addError(
new FieldError("registerDto", "confirmPassword"
, "Password and confirm password do not match")
);
}

AppUser appUser = repo.findByEmail(registerDto.getEmail());
if(appUser != null){
result.addError(
new FieldError("registerDto","email"
,"Email adress is already used")
);
}
I call the addError but the thing is on the client side, it appears in black and I would like to change the color or the font-size and I've done research to personnalize it but can't find any Oh wait it's basically a static file because it's a template or because I'm using thymeleaf ?
dan1st
dan1st5mo ago
static file means it's served without any modifications to it You have thymeleaf which processes/transforms it before sending it to the client Well, how are you reading the error in thymeleaf? You should be able to use CSS or similar things on it
Sung
SungOP5mo ago
Oh I thought static files meant no dynamic code actually 🫣
dan1st
dan1st5mo ago
? idk how exactly thymeleaf does it but with JSP, you are essentially generating Java code dynamically IIRC
Sung
SungOP5mo ago
Yes but I mean the terminology of static files let me check how I read the error in th
dan1st
dan1st5mo ago
Essentially with Thymeleaf, you define a template and thymeleaf renders the template (idk whether it uses some intermediate representation or other internals though)
Sung
SungOP5mo ago
Ok thanks for the explanation, I'm still a bit confused about the whole Spring Boot environement
JavaBot
JavaBot5mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
Sung
SungOP5mo ago
new FieldError("registerDto", "confirmPassword"
, "Password and confirm password do not match")
);
new FieldError("registerDto", "confirmPassword"
, "Password and confirm password do not match")
);
( sorry for the indentation ) I'm generating the message with the FieldError function
dan1st
dan1st5mo ago
So you are still talking about the errors displayed in a way you don't want, right?
Sung
SungOP5mo ago
Yes but I think I find it I think instead of writing codes I don't understand, I need to learn the entire Spring Boot architecture from the Controller to the DTO
dan1st
dan1st5mo ago
Yeah Spring has many things to it I think in isolation, most are pretty understandable but doing many at once are complicated On the other hand, that's the big advantage - Spring provides all of these things to developers
Sung
SungOP5mo ago
Yes I totally agree with you, I just find the solution to my problem but I can't really modify everything ( I've figured out how to change my error messages color, but the the style etc ) I'm still confused about the DTO and the entities to me it does the same thing
dan1st
dan1st5mo ago
Which problem? Is it the errors being displayed in a way you don't want or is it something different?
Sung
SungOP5mo ago
I mean I would to put other font style in the same attribute but this gives me an error ( it's the same thymeleaf attribute where I changed the color ) I tried to put an italic style but this rendered me an error
<p th:if="${#fields.hasErrors('phone')}" th:errorclass="text-red-700 " th:errors="${registerDto.phone}"></p>
<p th:if="${#fields.hasErrors('phone')}" th:errorclass="text-red-700 " th:errors="${registerDto.phone}"></p>
dan1st
dan1st5mo ago
What exactly did you change and what's the exact error it resulted in?
Sung
SungOP5mo ago
I've changed this class in a Tailwind class
th:errorclass="text-red-700 "
th:errorclass="text-red-700 "
dan1st
dan1st5mo ago
I don't think tailwind knows about Thymeleaf so adding a th:something in Tailwind probably isn't useful
Sung
SungOP5mo ago
and I've discovered that if you set the annotation with no message in the DTO, it render a default message in my native language ( whch is weird because I set the html tag in english It does at least for the color because I was able to change the color with a Tailwind utility class but I don't know if it's fully compatible between Thymeleaf and Tailwind tbh
dan1st
dan1st5mo ago
Instead of th:errorclass="text-red-700 ", can you try class="text-red-700"?
Sung
SungOP5mo ago
It doesn't render me in red anymore so I have to leave it that way
dan1st
dan1st5mo ago
?
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Sung
SungOP5mo ago
The utility class doesnt work anymore
dan1st
dan1st5mo ago
Which utility class?
Sung
SungOP5mo ago
The text-red-700, it's a Tailwind class
dan1st
dan1st5mo ago
Did you try with class= as well instead of just th:errorclass=? Does adding a custom style= work?
Sung
SungOP5mo ago
Yes but I want to add another class and it doesn't work, for example I'd like to add font-bold or font-italic behind the text-red but at run time it gives me an error
dan1st
dan1st5mo ago
what error are you getting?
Sung
SungOP5mo ago
It could work but this means I omit Tailwind
dan1st
dan1st5mo ago
and what's the code? it would be good to know whether it works for debugging
Sung
SungOP5mo ago
Let me do this tomorrow because I'm not at home rn, I will ping you with the code you want
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Sung
SungOP5mo ago
@dan1st | Daniel in this code, as I said yesterday, I tried to add another Tailwind class to modify my element
th:errorclass="text-red-700 font-semibold"
th:errorclass="text-red-700 font-semibold"
The font-semibold is the other class I wanted to add or font-italic, in both cases and based on the Tailwind documentation it should work but as you said maybe Thymeleaf is only compatible with only one class
dan1st
dan1st5mo ago
You still didn't specify whether it works with class instead of th:errorclass and also not whether non-Tailwind stuff works
Sung
SungOP5mo ago
no it didn't as I said before
dan1st
dan1st5mo ago
ah then I overlooked that Does it work with non-Tailwind stuff?
Sung
SungOP5mo ago
no problem, I think thymeleaf doen't allow me to have more than one utlity class
dan1st
dan1st5mo ago
because knowing that would help finding what causes the issue I don't really think that's the case Does the ont-semibold have any effect? Also can you check the HTML sent to the client in the devtools with th:errorclass="text-red-700 font-semibold"?
Sung
SungOP5mo ago
it basically change your element in bold I don't understand what you're saying, you mean checking it in the network devtool ?
dan1st
dan1st5mo ago
almost in the inspector
Sung
SungOP5mo ago
ok let me check after compile time
dan1st
dan1st5mo ago
though the network tab should work as well
Sung
SungOP5mo ago
Ok so at run time I have this exception which is directly in correlation with my tailwind classes
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "text-red-700 font-semibold" (template: "register" - line 56, col 70)
org.thymeleaf.exceptions.TemplateProcessingException: Could not parse as expression: "text-red-700 font-semibold" (template: "register" - line 56, col 70)
dan1st
dan1st5mo ago
ok yeah then you can probably not use th:errorclass there but that wouldn't explain class= not working (without a th:)
Sung
SungOP5mo ago
I think it's because I'm using a thymeleaf element that has a thymeleaf object as a parent but it's ok I think, it's not a big deal
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
JavaBot
JavaBot5mo ago
Post Closed
This post has been closed by <@680567289241927779>.
Want results from more Discord servers?
Add your server