Getting forbidden error all url in spring security (Spring boot)












0















I have configured spring security. I have created a login form, to sign in into my application. But getting forbidden error for any authorized "USER" or "ADMIN" role based url. But all permitAll permission url working fine.



In configuration file...



@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/bootstrap/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/home").hasRole("user")
.antMatchers("/admin").hasRole("admin")
.antMatchers("/**").denyAll()
.and()
.formLogin()
.loginPage("/")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.httpBasic()
.and()
.csrf()
.disable();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new BCryptPasswordEncoder());
}

@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}


In controller class



@Controller
public class HomeController {

@RequestMapping(value= {"/", "/index"}, method=RequestMethod.GET)
public String showHome() {
return "index";
}

@RequestMapping(value="/home")
public String login() {
return "home";
}

@RequestMapping(value="/admin")
public String showDashboard() {
return "dashboard";
}
}


In login form,



<form class="form-signin" th:action="@{/}" method="post">
...
...
...
</form>


In pom.xml



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>


N.B. The application is compiled and run successfully, no stack trace given. Just give me forbidden error.



I can't found, where I have made an error. Please, can you help me?

Thanks in advance.










share|improve this question























  • How do you load your role ? What's the sql ? Do you have the role define in the database ?

    – chaoluo
    Jan 1 at 15:00













  • I have two tables, one is, users table where the field name is username, password and enabled and another one is, authorities table where the field name is username and authority. I followed this tutorial, database part only. youtube.com/watch?v=uxbtIqaKsOA

    – Rashed
    Jan 1 at 18:42













  • @Rashed Possible duplicate of stackoverflow.com/questions/41946473/… or it is just a typo (lower case instead of upper case)?

    – dur
    Jan 1 at 21:48













  • no, I tried both upper and lower case but didn't work. @dur

    – Rashed
    Jan 2 at 3:57






  • 1





    The role name is ROLE_ADMIN or ADMIN ?

    – chaoluo
    Jan 2 at 4:21


















0















I have configured spring security. I have created a login form, to sign in into my application. But getting forbidden error for any authorized "USER" or "ADMIN" role based url. But all permitAll permission url working fine.



In configuration file...



@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/bootstrap/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/home").hasRole("user")
.antMatchers("/admin").hasRole("admin")
.antMatchers("/**").denyAll()
.and()
.formLogin()
.loginPage("/")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.httpBasic()
.and()
.csrf()
.disable();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new BCryptPasswordEncoder());
}

@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}


In controller class



@Controller
public class HomeController {

@RequestMapping(value= {"/", "/index"}, method=RequestMethod.GET)
public String showHome() {
return "index";
}

@RequestMapping(value="/home")
public String login() {
return "home";
}

@RequestMapping(value="/admin")
public String showDashboard() {
return "dashboard";
}
}


In login form,



<form class="form-signin" th:action="@{/}" method="post">
...
...
...
</form>


In pom.xml



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>


N.B. The application is compiled and run successfully, no stack trace given. Just give me forbidden error.



I can't found, where I have made an error. Please, can you help me?

Thanks in advance.










share|improve this question























  • How do you load your role ? What's the sql ? Do you have the role define in the database ?

    – chaoluo
    Jan 1 at 15:00













  • I have two tables, one is, users table where the field name is username, password and enabled and another one is, authorities table where the field name is username and authority. I followed this tutorial, database part only. youtube.com/watch?v=uxbtIqaKsOA

    – Rashed
    Jan 1 at 18:42













  • @Rashed Possible duplicate of stackoverflow.com/questions/41946473/… or it is just a typo (lower case instead of upper case)?

    – dur
    Jan 1 at 21:48













  • no, I tried both upper and lower case but didn't work. @dur

    – Rashed
    Jan 2 at 3:57






  • 1





    The role name is ROLE_ADMIN or ADMIN ?

    – chaoluo
    Jan 2 at 4:21
















0












0








0








I have configured spring security. I have created a login form, to sign in into my application. But getting forbidden error for any authorized "USER" or "ADMIN" role based url. But all permitAll permission url working fine.



In configuration file...



@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/bootstrap/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/home").hasRole("user")
.antMatchers("/admin").hasRole("admin")
.antMatchers("/**").denyAll()
.and()
.formLogin()
.loginPage("/")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.httpBasic()
.and()
.csrf()
.disable();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new BCryptPasswordEncoder());
}

@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}


In controller class



@Controller
public class HomeController {

@RequestMapping(value= {"/", "/index"}, method=RequestMethod.GET)
public String showHome() {
return "index";
}

@RequestMapping(value="/home")
public String login() {
return "home";
}

@RequestMapping(value="/admin")
public String showDashboard() {
return "dashboard";
}
}


In login form,



<form class="form-signin" th:action="@{/}" method="post">
...
...
...
</form>


In pom.xml



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>


N.B. The application is compiled and run successfully, no stack trace given. Just give me forbidden error.



I can't found, where I have made an error. Please, can you help me?

Thanks in advance.










share|improve this question














I have configured spring security. I have created a login form, to sign in into my application. But getting forbidden error for any authorized "USER" or "ADMIN" role based url. But all permitAll permission url working fine.



In configuration file...



@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
private DataSource dataSource;

@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers("/bootstrap/**");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/home").hasRole("user")
.antMatchers("/admin").hasRole("admin")
.antMatchers("/**").denyAll()
.and()
.formLogin()
.loginPage("/")
.permitAll()
.and()
.logout()
.permitAll()
.and()
.httpBasic()
.and()
.csrf()
.disable();
}

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource).passwordEncoder(new BCryptPasswordEncoder());
}

@Bean
public PasswordEncoder encoder() {
return new BCryptPasswordEncoder();
}
}


In controller class



@Controller
public class HomeController {

@RequestMapping(value= {"/", "/index"}, method=RequestMethod.GET)
public String showHome() {
return "index";
}

@RequestMapping(value="/home")
public String login() {
return "home";
}

@RequestMapping(value="/admin")
public String showDashboard() {
return "dashboard";
}
}


In login form,



<form class="form-signin" th:action="@{/}" method="post">
...
...
...
</form>


In pom.xml



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>


N.B. The application is compiled and run successfully, no stack trace given. Just give me forbidden error.



I can't found, where I have made an error. Please, can you help me?

Thanks in advance.







spring-boot spring-security






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 12:35









RashedRashed

7510




7510













  • How do you load your role ? What's the sql ? Do you have the role define in the database ?

    – chaoluo
    Jan 1 at 15:00













  • I have two tables, one is, users table where the field name is username, password and enabled and another one is, authorities table where the field name is username and authority. I followed this tutorial, database part only. youtube.com/watch?v=uxbtIqaKsOA

    – Rashed
    Jan 1 at 18:42













  • @Rashed Possible duplicate of stackoverflow.com/questions/41946473/… or it is just a typo (lower case instead of upper case)?

    – dur
    Jan 1 at 21:48













  • no, I tried both upper and lower case but didn't work. @dur

    – Rashed
    Jan 2 at 3:57






  • 1





    The role name is ROLE_ADMIN or ADMIN ?

    – chaoluo
    Jan 2 at 4:21





















  • How do you load your role ? What's the sql ? Do you have the role define in the database ?

    – chaoluo
    Jan 1 at 15:00













  • I have two tables, one is, users table where the field name is username, password and enabled and another one is, authorities table where the field name is username and authority. I followed this tutorial, database part only. youtube.com/watch?v=uxbtIqaKsOA

    – Rashed
    Jan 1 at 18:42













  • @Rashed Possible duplicate of stackoverflow.com/questions/41946473/… or it is just a typo (lower case instead of upper case)?

    – dur
    Jan 1 at 21:48













  • no, I tried both upper and lower case but didn't work. @dur

    – Rashed
    Jan 2 at 3:57






  • 1





    The role name is ROLE_ADMIN or ADMIN ?

    – chaoluo
    Jan 2 at 4:21



















How do you load your role ? What's the sql ? Do you have the role define in the database ?

– chaoluo
Jan 1 at 15:00







How do you load your role ? What's the sql ? Do you have the role define in the database ?

– chaoluo
Jan 1 at 15:00















I have two tables, one is, users table where the field name is username, password and enabled and another one is, authorities table where the field name is username and authority. I followed this tutorial, database part only. youtube.com/watch?v=uxbtIqaKsOA

– Rashed
Jan 1 at 18:42







I have two tables, one is, users table where the field name is username, password and enabled and another one is, authorities table where the field name is username and authority. I followed this tutorial, database part only. youtube.com/watch?v=uxbtIqaKsOA

– Rashed
Jan 1 at 18:42















@Rashed Possible duplicate of stackoverflow.com/questions/41946473/… or it is just a typo (lower case instead of upper case)?

– dur
Jan 1 at 21:48







@Rashed Possible duplicate of stackoverflow.com/questions/41946473/… or it is just a typo (lower case instead of upper case)?

– dur
Jan 1 at 21:48















no, I tried both upper and lower case but didn't work. @dur

– Rashed
Jan 2 at 3:57





no, I tried both upper and lower case but didn't work. @dur

– Rashed
Jan 2 at 3:57




1




1





The role name is ROLE_ADMIN or ADMIN ?

– chaoluo
Jan 2 at 4:21







The role name is ROLE_ADMIN or ADMIN ?

– chaoluo
Jan 2 at 4:21














1 Answer
1






active

oldest

votes


















0














Here is the default sql to load username, password and authority from database.



public static final String DEF_USERS_BY_USERNAME_QUERY = "select username,password,enabled "
+ "from users " + "where username = ?";
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "select username,authority "
+ "from authorities " + "where username = ?";


And role is just a authority that start with ROLE_, hasRole('XXX') is same as hasAuthority('ROLE_XXX')



So you can change the role name in database to ROLE_ADMIN,



or you can use hasAuthority('admin') instead of hasRole






share|improve this answer


























  • Thanks! It's working.

    – Rashed
    Jan 2 at 13:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995473%2fgetting-forbidden-error-all-url-in-spring-security-spring-boot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Here is the default sql to load username, password and authority from database.



public static final String DEF_USERS_BY_USERNAME_QUERY = "select username,password,enabled "
+ "from users " + "where username = ?";
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "select username,authority "
+ "from authorities " + "where username = ?";


And role is just a authority that start with ROLE_, hasRole('XXX') is same as hasAuthority('ROLE_XXX')



So you can change the role name in database to ROLE_ADMIN,



or you can use hasAuthority('admin') instead of hasRole






share|improve this answer


























  • Thanks! It's working.

    – Rashed
    Jan 2 at 13:57
















0














Here is the default sql to load username, password and authority from database.



public static final String DEF_USERS_BY_USERNAME_QUERY = "select username,password,enabled "
+ "from users " + "where username = ?";
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "select username,authority "
+ "from authorities " + "where username = ?";


And role is just a authority that start with ROLE_, hasRole('XXX') is same as hasAuthority('ROLE_XXX')



So you can change the role name in database to ROLE_ADMIN,



or you can use hasAuthority('admin') instead of hasRole






share|improve this answer


























  • Thanks! It's working.

    – Rashed
    Jan 2 at 13:57














0












0








0







Here is the default sql to load username, password and authority from database.



public static final String DEF_USERS_BY_USERNAME_QUERY = "select username,password,enabled "
+ "from users " + "where username = ?";
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "select username,authority "
+ "from authorities " + "where username = ?";


And role is just a authority that start with ROLE_, hasRole('XXX') is same as hasAuthority('ROLE_XXX')



So you can change the role name in database to ROLE_ADMIN,



or you can use hasAuthority('admin') instead of hasRole






share|improve this answer















Here is the default sql to load username, password and authority from database.



public static final String DEF_USERS_BY_USERNAME_QUERY = "select username,password,enabled "
+ "from users " + "where username = ?";
public static final String DEF_AUTHORITIES_BY_USERNAME_QUERY = "select username,authority "
+ "from authorities " + "where username = ?";


And role is just a authority that start with ROLE_, hasRole('XXX') is same as hasAuthority('ROLE_XXX')



So you can change the role name in database to ROLE_ADMIN,



or you can use hasAuthority('admin') instead of hasRole







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 13:58

























answered Jan 2 at 11:49









chaoluochaoluo

1,6301916




1,6301916













  • Thanks! It's working.

    – Rashed
    Jan 2 at 13:57



















  • Thanks! It's working.

    – Rashed
    Jan 2 at 13:57

















Thanks! It's working.

– Rashed
Jan 2 at 13:57





Thanks! It's working.

– Rashed
Jan 2 at 13:57




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53995473%2fgetting-forbidden-error-all-url-in-spring-security-spring-boot%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter