157 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
fsl4faisal
10 years ago

No bean named ‘dataSource’ is defined..I got this error

Chaitnahya
8 years ago
Reply to  fsl4faisal

Create a database name with “test” and create tables there.Refer 4.1 and 4.2 in this page.

Pankaj
7 years ago
Reply to  Chaitnahya

i already did that but still geting same error. Please suggest.

Imran Rajjad
10 years ago

Its working on Windows7 but on WindowsServer 2012R2, it says invalid login, when the database , jdk and tomcat are same on both machines. What could cause this?

avadhoot
10 years ago

sorry something went wrong i wanted to to ask the question in this way:
HI Mkyong, can you please explain me <form name='f' action="” method=’POST’>
and why we declare method=post here because In controller class we declare
@requestmapping(value=”/login”,method=RequestMethod.GET) then what is the use of this ‘j_spring_security_check’ url.

Sdefdfsdf
7 years ago
Reply to  avadhoot

The whole program is showing one error Classs listener not found

Mallesh
10 years ago

i got this exception in annotation example do you know what is my mistake…? java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered?

Prasad
10 years ago

Hi mkyong,

Nice tutorial. sir.. i download that code(” spring-security-login-form-database-xml “) i configured on my local mechain i am using Eclipse+tomcate7+maven + Database(Oracle 11c)+jave7 now i able to open the login page sir but i am using login page. when i try to login (mkyong/123456) but it was showing invalid usename and password. but i am inserted values on database as well. can u plz tell me sir.

Thanks & regards,
Prasad.

ambrish singh
9 years ago
Reply to  Prasad

manage to get the solution ?

Derbali Chaymae
10 years ago
Reply to  Prasad

same here! did u resolve the problem please ? (already tried to update my database password)

ambrish singh
9 years ago

did you get the solution for this, i tried 1 as enabled with oracle didn’t worked

amine
10 years ago

you need to run the sql file

Ondrej Tokár
10 years ago

Same here…

rahul
8 years ago

http status 403 – access is deniedtype status reportmessage access is denieddescription access to the specified resource has been forbidden.
error occurred after givening right email and password
any help highly acceptable

srinivas nangana
9 years ago

Hi Mkyong after pressing logout link It is logging out successfully but if press back button still allow us to access home page.

Ananda Bayu
10 years ago

hi Mkyong,
thank for the source code, its working for me but the other POST METHOD is not Allowed by SpringSecurityFilterChain like i want to add user the console said 405 method not allowed, all the POST METHOD has error 405 after i add spring security.

can you solved it?
Thanks.

Pramod Gaikwad
10 years ago

Hi MkYong, I am stuck at configuring spring security with Spring Boot.
Actually my problem is that i want to build secured application with my custom login form and user should authenticated after successful login. also i want to enable csrf protection for REST url’s. If you have already developed this then please guide me. Thank you for appreciating.

bhuvana
9 years ago

this code is not working can any one tell me what i need to do after importing

Samkova
9 years ago

Your code doesn’t work; I downloaded the project and also tried to copy/paste the code from this page, but the result was the same. I’d better look for another tutorial somewhere else.

Kumar Abhilash
9 years ago
Reply to  Samkova

Any solution?

invalid_user
9 years ago

Invalid username and password! all the time. Database is alright, done with the script provided, populated with the data provided. I’m using the annotation app.

Kumar Abhilash
9 years ago
Reply to  invalid_user

Have you found any solution?

carlos alberto cortez polanco
9 years ago

support ssl connect postgresql?

Ankit Lalan
9 years ago

For both the users, when I use http://localhost:8080/admin** it provides me access to Logout Link.

How can we differentiate on the basis of user’s right.

Mkyong and Alex both users have got the ability to view the page when its /admin** and also /logout can be accessed when we hit the url directly in the browser.

???? ?
10 years ago

For all those who faced 405 Post not allowed in this example – just add POST to request methods annotation of 403 mapping in controller.

Anusha Cheluvagopal
10 years ago

Hey, this tutorial worked perfectly for me except for the logout part. I even get redirected to the login page with the logout message but when I try to accessing the restricted URL (/admin in this case), I was still able to see the page. Shouldn’t the session be destroyed and the 403 page be displayed?

Yogesh Chavan
10 years ago

Hi MkYong , do we have same tutorial but with angularjs ? please suggest. thanks

amna jebali
10 years ago
Reply to  Yogesh Chavan

hi yogesh , me too i need the same tutorial with angulrajs?? can you help me??

Aakash
10 years ago

Can you please help to create database table structure with Oracle database as it does not have

TINYINT type datatype.

Hyd Balu
10 years ago

Hi Yong,
how to navigate to user page and Admin page though the login page itself. Not accessing the Admin page, then enter the credentials and the Admin page is displayed. How can we directly navigate from login page to the respective pages based on the role assigned in the database table.

NAVEEN YADAV
10 years ago

Hi,
How to provide url encoding spring mvc application
i need your help please.

NAVEEN YADAV
10 years ago

Hi,
how to provide http to https in spring mvc application

Xena
10 years ago

hi Mkyong, I tried all of your examples of Spring Security both in Windows and Linux server, except this one with MySql worked, this one works in Windows, but not in Linux. I saw someone else had the same problem but there were no reply. Would you please tell us something. Thank you for this post.

Xena
10 years ago
Reply to  Xena

Hello there. I found the solution, in Linux environment localhost should be written as 127.0.0.1. Thank you again for this post.

suma shetty
10 years ago

how to integrate user sessions with this, as per my requirement i have 4 different pages like Dashboard, Live data, Historic data and notifications page.

this is how spring-security has been configured

and loginController as shown below

@Controller
@SessionScoped
public class LoginController {
@RequestMapping(value = “/dashboard**”, method = RequestMethod.GET)
public ModelAndView dashboardPage() {
ModelAndView model = new ModelAndView();
model.setViewName(“dashboard”);
return model;
}
@RequestMapping(value = “/historical**”, method = RequestMethod.GET)
public ModelAndView livePage() {
ModelAndView model = new ModelAndView();
model.setViewName(“history”);
return model;
}
@RequestMapping(value = “/live**”, method = RequestMethod.GET)
public ModelAndView historyPage() {
ModelAndView model = new ModelAndView();
model.setViewName(“live”);
return model;
}
@RequestMapping(value = “/notifications**”, method = RequestMethod.GET)
public ModelAndView notificationsPage() {
ModelAndView model = new ModelAndView();
model.setViewName(“notifications”);
return model;
}
@RequestMapping(value = “/login”, method = RequestMethod.GET)
public ModelAndView login(@RequestParam(value = “error”, required = false) String error,
@RequestParam(value = “logout”, required = false) String logout) {
ModelAndView model = new ModelAndView();
if (error != null) {
model.addObject(“error”, “Invalid username and password!”);
}
if (logout != null) {
model.addObject(“msg”, “You’ve been logged out successfully.”);
}
model.setViewName(“login”);
return model;
}
//for 403 access denied page
@RequestMapping(value = “/403”, method = RequestMethod.GET)
public ModelAndView accesssDenied() {
ModelAndView model = new ModelAndView();
//check if user is login
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (!(auth instanceof AnonymousAuthenticationToken)) {
UserDetails userDetail = (UserDetails) auth.getPrincipal();
model.addObject(“username”, userDetail.getUsername());
}
model.setViewName(“403”);
return model;
}
}

So with this configuration, only first time on on deploying the app irrespection of the req i make appname/dashborad, appname/historical, appname/live, appname/notifications , it is rediecting to login page as i have mapped /login in rediect page, after that i can directly access other pages say once i login and logout 🙁

How to make it secured, i mean i dont want other pages to be accessed unless it is authenticated 🙁

Anon
10 years ago

Hi!
I think that in your first SecurityConfig.java you want to say AppConfig.

Good tut!
Thanks

eviltoad
10 years ago

Thank you, this tutorials of yours are always clear, simple to follow and very useful.

NaN
10 years ago

I like your examples!

TGal
10 years ago

Hey,
I got an exception (dataSource not found) I think in spring-security.xml you have to add import tag:

Rohit
5 years ago

It helped me. Thanks

I’ve used .hasRole(“USER”) instead of .access(“hasRole(‘ROLE_ADMIN’)”)

David Kham
6 years ago

hi Mkyong
please you can help me. I would like to use spring security with AES256. Is it possible and how to use?

msryhm
6 years ago

the best shit code