简体   繁体   中英

Spring Security - doesn't access database

I am using spring boot and very new to spring security, but I wanted basic security to my web application. What I did was add on my pom.xml the needed dependencies and added this java class to my project:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {

    auth
            .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .antMatchers("/**","/event/**","/ticket/**")
            .hasRole("USER")
            .and()
            .formLogin();
}

}

After running my web application, I run into the login page, where I put user/password and then it goes to my web application. However, the commands don't work. I am pushing some buttons that should send signals to my MySql database, but nothing happens. It's like the front-end isn't connected to the back-end anymore. I am using AngularJS for front-end and a View Controller that navigates between pages. Rest of the application is REST-based. Any idea why this might happen?

Later Edit: Now I understand, the problem that I have is that after authenticating, I get 403 status codes on my end-points. Any idea how I might fix it?

Later Editv2: Looks like I don't get authorized on my POST requests, my GET ones work fine...here are some of my POST end-points: /event/buy_ticket/{id} , /ticket//cancel_ticket/{id}

angular.min.js:101 POST http://localhost:8080/event/buy_ticket/2 403 ()

I even tried to explicitly say it to permit it, but I still get 403...

http.authorizeRequests()

.antMatchers("/**","/event/**","/ticket/**","/event/buy_ticket/2")
            .permitAll()
            .and()
            .formLogin();

Later later later edit:

Disabling csrf worked

Getting 403 Forbidden error codes means that Spring is receiving your requests but choosing to stop processing them. From the Wiki page on HTTP 403 :

Authentication was provided, but the authenticated user is not permitted to perform the requested operation.

If I had to wager, I would say the problem is that you have not specified what resources and endpoints should be accessible and how. If memory serves me right, Spring Security will, by default, lock down everything super tightly so you need to explicitly tell it what to leave open. Below is a sample from my own security configuration:

@Override
protected void configure(HttpSecurity http) throws Exception {    

  http
     .authorizeRequests() // require authorization
     .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() // for the CORS preflight check
     .antMatchers("/login", "/api/open/**", "/resources/**").permitAll()  // the open API endpoints and resources
     .antMatchers("/logout", "/api/secured/**").authenticated(); // lock down these endpoints

  ...additional configurations...
}

All endpoints that should be freely available are prefaced with "/api/open/" while all endpoints that should be protected by Spring Security are prefaced with "/api/secured/" . The exceptions are the logout and login endpoints, since those tie into Spring Security directly.

Here's a great blog post - and the related repo - that shows off how to implement Spring Security that plays nice with AngularJS, even as a Single Page Application, which are notoriously annoying to secure.

Edit: You might be running up against CSRF protection, which is enabled by default in Spring Security - see this post . CSRF will allow HTTP "safe" methods like GET, HEAD, and OPTION but block "unsafe" methods like PUT, POST, DELETE, etc if those methods do not provide the proper token (since no CSRF was configured, those request don't have a token -> blocked). For testing purposes, you can disable it by adding http.csrf().disable() to the configure() method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM