39 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
vinayak
8 years ago

Hello, How can i integrate spring security in Spring boot rest service and Java Swing(accessing API’s using Apache HttpClient)

FaisalHyder
10 years ago

sorry to say but this is an easy way out… u should have put css and js folders separately inside src/main/resources folder and then have shown how to use them inside our Spring Security application with @EnableWebMVC.

Premnath
11 years ago

When I execute as it is, after entering username and password I am getting below error

HTTP ERROR 403

Problem accessing /login. Reason:

Expected CSRF token not found. Has your session expired?

Ram
8 years ago

Hi

Thanks for the tutorial it was very helpful. But when I use the same configuration in production where I deploy the application in example.com. The redirection when I hit example.com/admin without logging in goes to localhost:8080/login. I understand why this is happening since Spring is running under localhost in the server but is there a way to fix this to redirect to example.com/login instead?

Please note I tried adding baseUrl to loginPage like loginPage(baseUrl + ‘/login’) but that results in infinite loops.

Thanks

shweta chugh
9 years ago

Hi,
I am running two applications UI and UIManager. Both the applications are running at different port. Spring Security is implemented at UIManager application but login page is set in UI application. Spring security works fine in Chrome, but in Mozilla firefox, spring security is not working, Firefox is unable to send a request from UI app to UIManager app. And in IE browser, windows security manager pop up comes after I enter credentials in login page. Please give me the solution to this.

carlos
9 years ago

This solution is BAD, how to use ApplicationEvent with spring security

crewdoo
10 years ago

Hi. How can I get access to _csrf.token if i using freemarker templating engine?

Guna
10 years ago

You have only given the entire program, please also explain as to why we have to use these functions and what does it do..
merely just copying and pasting ur code and running the program is not helping us

Satish Pakalapati
10 years ago

HI Mkyong,

can you please explain below question :

how to prevent multiple users login with same username and password in spring?

Satish Pakalapati
10 years ago

Hi,
can you plz explain below question :

how to prevent multiple users login with same username and password in spring security.

Willians Martins
10 years ago

Hello mkyong, I’m loving your posts, I copy this code tutorial and is running beautiful, but now, I want to use AuthenticationManagerBuilder getting the value of database, I get this code from another your tutorial, When the password is fixed(encrypt) on the database it is ok, but when I try to save in database one another user the password is write without encrypt(plaintext), can you understand me and help me?

yas
10 years ago

This is for those of you who try to learn and make the best of it.

If you are using latest spring security which is “4.0.2.RELEASE”

and if you get blocked by http:xxxx/server_name/context_path/j_spring_security_check

add this to http in SecurityConfig “loginProcessingUrl(“/j_spring_security_check”)” thanks to Denis Ageev

and if you get blocked by http:xxxx/server_name/context_path/j_spring_security_logout

There are two ways possible:

1) .logoutRequestMatcher(new AntPathRequestMatcher(“/j_spring_security_logout”)) in SecurityConfig as the same way you did /j_spring_security_check

2) you put this
insetad of in admin.jsp

this is because of this reason
http://docs.spring.io/spring-security/site/docs/4.0.2.RELEASE/reference/html/jc.html#jc-logout

it says as follow

The URL that triggers log out to occur (default is /logout). If CSRF protection is enabled (default), then the request must also be a POST. For for information, please consult the JavaDoc.

johaness vix
11 years ago

this is not very flexible if you need for example angular js for doing auth besides those html ugly spring tags.

Aspen Sugan
11 years ago

Can anyone please reply me asap.

Jeff
11 years ago

Hello Mr Mkyong

When I run the app on sever (Tomcat), in the browser I get this message “Ressource not available” , and the url is “http://localhost:8002/spring-security-loginform-annotation/”

What’s the problem ?
Please help me. Thank you

PJ
11 years ago
Reply to  Jeff

Check your main console (Tomcat) and check status of your application.

Aman jajodia
8 years ago
Reply to  PJ

I am also facing the same probelm and its just showing servlet startup at the end

akshobhya kallapur
11 years ago

Hi , thanks for this example .. but when I give correct username and password it is going to index page, May I know y?

Guest
11 years ago

Thank you, good tutorial

Nacho
11 years ago

Not working…

Abul Fayes
11 years ago

The username parameter is set as “username” and the password parameter is set as “password” in SecurityConfig.java but in the login.jsp the input fields uses “user” and “pass” for the name attributes so it didn’t work for me without changing the input name attributes to match the parameter values. Otherwise a great tutorial thanks!

Juliuz
11 years ago

Hello, the text for bad credentials and succesful logout isent in colour. Can you help?

Pawel
11 years ago

wery good! It’s really worked!

Priyanka Kulathilaka
11 years ago

I got error and posted here it

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project : Compilation failure: Compilation failure:
[ERROR] ..SecurityConfig.java:[12,7] error: cannot access Filter
[ERROR] ..SpringSecurityInitializer.java:[5,7] error: cannot access ServletException
[ERROR] -> [Help 1]

http://stackoverflow.com/questions/25073074/error-cannot-access-filter

Girish
9 years ago

javax.servlet
javax.servlet-api
${javax.servlet.version}
provided

scope is incorrect, it was
provider

Tushar Bhatt
11 years ago

I am not able to deploy the application in Tomcat, on researching I found that there is an error in SecurityConfig.java file. Error is : ”
Could not autowire. No beans of ‘AuthenticationManagerBuilder’ type found.
“. Can somebody help me to resolve the issue.

Virmerson Santos
11 years ago

To function correctly, and avoid 404 error I switched j_spring_security_logout by “/ logout” and also j_spring_security_check by “/ login” in login.jsp file and admin.jsp

Denis Ageev
11 years ago

add this to http in SecurityConfig “loginProcessingUrl(“/j_spring_security_check”)”

RaunakShakya
11 years ago
Reply to  Denis Ageev

thank you. your suggestion helped me solve a problem that had been cracking my brains for the past few days.

Philip Ardley
11 years ago

The ” spring-security-custom-login-form-annotation.zip (19 KB)” can’t be imported to Eclipse ad run on Tomcat 7. These are the steps I took to make the imported (from file system) project work:

1) In the pom.xml change the javax.servlet.version from “3.1.0” to “3.0.1”.
2) In the pom.xml change the jdk.version from “1.6” to “1.7”.
3) Open the project properties, -> Java Build Path -> Libraries, remove all those links to the maven repository.
4) Right click your project, -> Configure -> Convert to maven.
5) Make sure your Tomcat library is in Java Build Path -> Libraries and also is the target runtime.

6) Run the project on the server like normal.

There are very often these types of problems with maven example projects from this website.

Guest
11 years ago

The “last Spring Security hello world annotation example” can’t be imported to Eclipse ad run on Tomcat 7. These are the steps I took to make the imported (from file system) project work:

1) In the pom.xml change the javax.servlet.version from “3.1.0” to “3.0.1”.
2) In the pom.xml change the jdk.version from “1.6” to “1.7”.
3) Open the project properties, -> Java Build Path -> Libraries, remove all those links to the maven repository.
4) Right click your project, -> Configure -> Convert to maven.
5) Make sure your Tomcat library is in Java Build Path -> Libraries and also is the target runtime.

6) Run the project on the server like normal.

There are very often these types of problems with maven example projects from this website.

Lurkin
12 years ago

This tutorial is not working 🙁

I got red mark in eclpse on………..

1.@ComponentScan({ “com.mkyong.web.*” })

2.auth.inMemoryAuthentication().withUser(“mkyong”).password(“123456”).roles(“USER”);

3.AbstractAnnotationConfigDispatcherServletInitializer

4.AbstractSecurityWebApplicationInitializer

The jars I’m using are:

javax.servlet-api-3.0.1.jar

jstl-1.2.jar

org.springframework.context-3.0.4.RELEASE.jar

spring-beans-3.0.4.RELEASE.jar

spring-context-3.0.2.RELEASE.jar

spring-core-3.1.1.RELEASE.jar

spring-security-config-3.2.4.RELEASE.jar

spring-security-core-2.0.2.jar

spring-security-web-3.0.0.RELEASE.jar

spring-web-3.0.5.RELEASE.jar

spring-webmvc-4.0.5.RELEASE.jar

Raghavulu Bussa
12 years ago

Hi,

I’m getting

No mapping found for HTTP request with URI [/{app-context}/j_spring_security_check] in DispatcherServlet with name ‘dispatcher’ when using the java config while it worked with xml config

JukaBarros
11 years ago

If you use Spring Security 3.2.x you should change “j_spring_security_check” to “login” and “j_spring_security_logout” to “logout”

And the parameter (username and password) in SecurityConfig.java must have the same name in input in login.jsp. Example:

SecurityConfig.java

usernameParameter(“name”).passwordParameter(“pass”)

login.jsp

User:

Password:

RiccardoC
11 years ago

as far as I know, this example won’t work with Spring Security 3.2.x; they changed some urls.

j_spring_security_check is now /login (POST only, unless you configure it differently)

you have to provide your mapping for logout, this is is accomplished by something like (after usernameParameter(“username”).passwordParameter(“password”)):

.and().logout().logoutUrl(“/logout”);

Juan Manuel Flores
11 years ago

Same error. Could you make it work?

Bono
12 years ago

I’ve got the same error. Nice example but not working properly.

spring-context, spring-webmvc: 4.0.5.RELEASE
spring-security-core, spring-security-config, spring-security-web: 3.2.4.RELEASE

??????? ???????
12 years ago

Hi, why you don’t use the latest spring 4.

johaness vix
11 years ago

he doesn’t know how to.