61 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Ed Hail
9 years ago

Thank you for putting this together.
When I try to run this code from a command line I get an error:
org.thymeleaf.exceptions.TemplateInputException: Error resolving template “/login”, template might not exist or might not be accessible by any of the configured Template Resolvers

san
8 years ago
Reply to  Ed Hail

hey check templete folder structure

Angelo Guzzo
8 years ago

Hi. Nice post. I want just to tell you were missing the dependendy for jquery which is mandatory for bootstrap. You should add it in the pom.xml

org.webjars
jquery
3.2.1

Pablo Borges
8 years ago
Reply to  Angelo Guzzo

Thank you!

Vardhman
8 years ago

Hi,
I need configuration which is in spring boot, jwt token security and thymeleaf, mysql with different userwise login success page. this project must run from java cmd by jar, without external tomcat….Please hepl us

Niranjan
5 years ago

This is not working. After I enter username and password of admin, it again redirects me to login page.

HAFSSAELOIAABANE
7 years ago

hello thank u so much for your tuto it’s so interesting i need your help i wnat know if i can change the content in application.properties with the habitual content(spring.datasource.url=jdbc:mysql://localhost:3306/database
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.testWhileIdle=true
spring.datasource.validationQuery=SELECT
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

Alex
8 years ago

Hi! I used you code and getting error ‘No beans of ‘AccessDeniedHandler’ type found.’ in

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
/**
*
*/
@Autowired
private AccessDeniedHandler accessDeniedHandler;
….
}

Krista Mutyaba
8 years ago
Reply to  Alex

Add @Component annotation on the MyAccessDeniedHandler.java class

Justyna
8 years ago

Hi!
Thank you for great tutorial.
I have a question. Why do you set “csrf()” to “disable()”?

Phea
7 years ago
Reply to  Justyna

Hello Justyna,

I have just tried to look around the blogs related to Spring. In this moment I saw your comment. So let’s see my answer.

csrf().disable(); it’s intercept that spring security to disable csrf token. Normally we need to enable it for prevent CSRF. So when the browser request to web application, our web application will generate the csrf token to the browser for store in the cookie. So when the browser request again something like post our web server will check csrf token match or not!

aji
7 years ago
Reply to  Phea

Hello Justyna,
how do we know the csrf token is a match or not? is it handled by spring automatically? or that thing should be handled manually?

Secure
8 years ago

How does the Logout work ? There is no logout mapping ?

Manhnguyen
8 years ago
Reply to  Secure

If u wanna show logout page, u should config on SpringSecurityConfig:
Example:
.logout().logoutRequestMatcher(new AntPathRequestMatcher(“/logout”)).logoutSuccessUrl(“/”)

Vinicius Branco
9 years ago

Add the following code to SpringSecurityConfig.java to be able to load CSS and “webjars/bootstrap/3.3.7/css/bootstrap.min.css” when not authenticate!

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(“/webjars/**”);
web.ignoring().antMatchers(“/css/**”,”/fonts/**”,”/libs/**”);
}

Vinicius Branco
9 years ago
Reply to  mkyong

Didn’t work for me without this code.

When authenticated load webjars perfectly, but when not authenticated, webjars is missing and breaks the layout.

Justyna
8 years ago

I have the same problem – but When authenticated AND when not authenticated.

Justyna
8 years ago
Reply to  Justyna

I noticed, in chrome works better but in IE almost perfectly – without blinking of subpage.

sam
3 years ago

Hello Young. a very good tutorial.
I compiled and and ran it, and found a small issue with loading the local /css/main.css file. I used Inspect in chrome browser and found the main.css is not getting loaded, with error message in the console:
“Refused to apply style from ‘http://localhost:8080/login’ because its MIME type (‘text/html’) is not a supported stylesheet MIME type, and strict MIME checking is enabled.”
Do you know why?

Nil
5 years ago

In this project i am getting some issue when I login as admin/user it redirect me to http://localhost:8080/webjars/bootstrap/3.3.7/js/bootstrap.min.js ,which means it show me botstrap internal codeing, when remove all except localhost:8080 it show me actual home page. I code same as you but why am getting this type of issue

macleo
7 years ago

Could you please explain where are the parameters param.error and param.logout variables come from in this project?
thanks mkyong.

macleo
7 years ago

where the variable ${param.error} come from ?
thanks

naveenkumar mark
7 years ago

Hi, This is really useful.

I faced some issues when I tried this sample with my test application
@Controller
@RequestMapping(“/thyme”)
public class ThymeController {
@GetMapping(“/”)
public String home() {
return “/home”;
}
}

when I added this (“/thyme”) mapping I used to get only Access Denied Error page only.
After I removed it, everything is normal.

Should not I use any other mapping? Can someone give me some explanation on this?

Please add Password Encryption, I faced issue here.
I had to change from this
.withUser(“user”).password(“password”).roles(“USER”)

this
.withUser(“user”).password(“{noop}password”).roles(“USER”)

Thanks!

coder
8 years ago

can u explain the code as well? I mean what is happening in here?

jun
4 years ago

thank you. this post is really helped me.

Alsaner
6 years ago

Hi!
Why do you switch off @EnableWebSecurity in SpringSecurityConfig class? (in your zipped code)
When switching on, I can’t see styles in all html.
Why?

suresh
6 years ago

Good blog really helped me a lot.

dee
6 years ago

what is spring boot?

NimbleMarty
7 years ago

Great tutorial. Missing JQuery webjar. it is a bootstrap dependency. Still works

naveen
7 years ago

Hi, I always get access denied error page, I followed your code exactly but could not do anything

Pavan Kumar Akula
7 years ago

thanks for the post, i cant get the thymeleaf sec:authentication=”name” to work , its always blank . i am using spring boot 2.1.2 and spring security 5 with thymeleaf extras.

Sachin Puri
5 years ago

Add following dependency

groupId org.thymeleaf.extras
artifactId thymeleaf-extras-springsecurity5

Madhava
7 years ago

i am getting Bad Credentials exception and login does not work with the code that i downloaded from here. Any help?

Jimmy
7 years ago
Reply to  Madhava

please check, if “SpringSecurityConfig” class has “@Configuration” annotation

Mihaela
7 years ago
Reply to  Jimmy

I had the same problem and i was allowed to enter the homepage only using the generated security password, not the one set in the SpringSecurityConfig class (password)

Prince Grover
7 years ago

I am authenticating the user using one of the post method in the controller. I am not using inMemoryAuthentication(). So in that case how do I proceed

Roberto Fonseca
8 years ago

Hi, nice post, as usual!
Do you have some material about testing Controllers with Thymeleaf and Security?

Ichroman Raditya
8 years ago

Hello!
I just followed your tutorial. Everything seems ok, I just a little problem. The static resources (css, js) were not loaded at first, untill I modify the SpringSecurityConfig, added /static and /webjars to be permitted in antMatchers. Now I think it has resolved the issue, but the main.css still not be loaded. When I see the javascript console log, it said that ‘Refused to apply style from ‘http:80801/login’ because it’s MIME type…’.

I’ve googled it but still haven’t found any fit answer.

Thanks.

HersheySquirt
8 years ago

Did you update to a newer version of the webjars in in your pom? If so, you will need to update the version references in your html files. I had the same problem and that fixed it for me.

Long
8 years ago

Hi. I downloaded your project and import to Eclipse as Existing Maven Project. I build and started successfully but i got below error:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Mar 20 11:51:03 ICT 2018
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template “/login”, template might not exist or might not be accessible by any of the configured Template Resolvers

Can you please help?
Regards,
Long

Gege
8 years ago

I can’t run your example from GitHub, I’m getting this:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Mar 09 16:08:49 CET 2018
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template “/login”, template might not exist or might not be accessible by any of the configured Template Resolvers

Gege
8 years ago
Reply to  Gege

org.thymeleaf.exceptions.TemplateInputException: Error resolving template “/home”, template might not exist or might not be accessible by any of the configured Template Resolvers
,

TungstenX
8 years ago
Reply to  Gege

Check in DefaultController.java and remove the / from the return string

Alim
8 years ago

Nice tutorial, thank you for sharing !

Sachin Kale
8 years ago

In Chrome it did not work even after clearing browser cookies. Tried in Firefox, it worked as expected.