Spring security returning 401 with requestmatchers permitAll
When i try registering a new user it only works if i pass credentials of an user that is already registered through basic auth. If i try passing a register user request without auth it returns 401.
security config:
82 Replies
⌛
This post has been reserved for your question.
Hey @Victor! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose Post
button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.

💤
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.
anyone?
can you elaborate
you mean your request to api v1 register is unauthorized even tho its permitall right
show the controller
yea exactly
if i pass credentials the register goes through, which makes me think the permitAll isnt working
what if you comment out the line with oauth2
does it change anything
i'll try it in a bit
dont i need that for jwt auth tho?
ye but just for testing
to see if removing it helps
btw using the if present in your code gonna cause race condition
unless you use some locking
or just add a unique constraint to the username
yea i was going to do that
thats so 🎿 🐝 🦌 then
also check if permitting any request to register will throw the same error
same results with the oauth line commented
you mean without specifying just post?
without specifying http method
401 still
quite interesting
you sure you put request mapping in the controller
when i was googlin this problem i saw someone mentioning something about removing anyrequest authenticated and doing it on a method basis(?)
something like this
yea u didnt add request mapping
add
above rest controller
i have it on the application properties
i think if the pathing was the problem it wouldnt work with the authentication
this controller and the other controller both work normally if i pass authentication
oh
try instead of .authenticated
do

hmmmm
show how you set it up in application properties
the request mapping
server.servlet.context-path=/api/v1

o mamma mia
ok so
do this instead
in app properties
spring.mvc.servlet.path=/api/v1
is that wrong?
use this instead of what u pasted
and see if it helps

i dont think the path is the problem because the controller works still
my bad
it was a dot instead of a dash
try this
it works but i still get 401
weird
that shouldve helped
whats the difference between that and what i had before?
pathinto is null for the thing you sent
ok wait lets call the special forces of java 😱
@dan1st | Daniel can you take a look
please 😭
i really dont get whats going on
Spring security DEBUG or TRACE logs?
also you got the validated annotation in the controller method btw
Can I see the relevatn current code, config, exact error and logs with https://stackoverflow.com/a/47729991/10871900 set?
is it ok if i send the logs in .txt?
yes
Should the endpoint require authentication?
Can you show the security config?
im trying to have no auth needed to register and auth to log in
the controller:
Can you show the directory structure of your application?
its probably a bit messy

hm, looks fine
maybe its oauth being skibidi
and its config
It's a POST request, right?
yes
the request works fine if i pass basic auth of an already registered user
you might need CSRF
nvm you disabled it
yea thats what i found most people suggesting when i searched the issue
Does commenting out
.httpBasic(Customizer.withDefaults())
change anything?
just for testingstill get 401
Can you check whether your controller is called?
wait
Can you show your application.properties?
jwt.private.key=classpath:app.key
jwt.public.key=classpath:app.pub
spring.application.name=resell-platform
spring.mvc.servlet.path=/api/v1
spring.datasource.url= jdbc:postgresql://localhost:5432/resell
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
logging.level.org.springframework.security=DEBUG
You aren't using
@RequestMapping
, right?
for specifying the path on the controllerim not but both this controller and the other controller work fine if authentication is passed

In your security config, replace
"/api/v1/register"
with "/register"
alright that worked lol
i definitely had tried that already but i think i had something else messing it up at the time
ye i think it was that servlet too
that might be colliding before
the old path on the properties?
Did you have it before adding
spring.mvc.servlet.path=/api/v1
?you can check
So the
spring.mvc.servlet.path=/api/v1
means Spring adds that everywhere essentiallyif it changes anything
i was using server.servlet.context-path=/api/v1 instead before
so if you do
spring.mvc.servlet.path=/api/v1
, you should have the /api/v1
on neither the controller nor the security configlets see
works with the old path too, i must have messed something up else before
thank you both
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.
@dan1st | Daniel you're the goat
btw @ayylmao123xdd if you ping me in the future for things in #java-help, it would be easier for me if you just add a short summary to the message you are pinging me with
whats the problem?
it doesnt actually validate the stuff ur sending
i just noticed that
use valid instead
@Valid
valid doesnt show for me, am i missing a dependency?
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
and in the dto you are sending to controller you can use @NotEmpty on string etc
@ayylmao123xdd can you accept my friend request real quick? so we dont steer away from the topic of the post too much?
ok
Post Closed
This post has been closed by <@148254954547576832>.