fish
fish

Reputation: 11

Spring-Security 5 always 302

I am trying to test my web api thats secured using the standard Spring Security API,However whenever I login to my application the /test.html api keeps returning a 302 redirect. username:admin / password:admin

enter image description here

enter image description here

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping("/hello")
    public String hello() {
        return "hello";
    }
}

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@Configuration
@EnableWebSecurity
public class BrowserSecurityConfig extends WebSecurityConfigurerAdapter {

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

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/test.html").permitAll()
                .loginProcessingUrl("/user/login")
                .and()
                .authorizeRequests()
                .antMatchers("/test.html").permitAll()
                .anyRequest()
                .authenticated();
    }
}

package com.example.demo;

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

@Component
public class UserDetailsServiceImpl implements UserDetailsService {

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        return new User("admin",
           "$2a$10$vs7veyVUaqeGyVlxXpp94O7BcmzcF2HGUmH2va6XDVCj2mK8uFzRi",
           AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}

https://github.com/woshituotuo/demo.git

Upvotes: 1

Views: 3862

Answers (3)

Ruwan Prasad
Ruwan Prasad

Reputation: 26

Exclude SecurityAutoConfiguration.class and ManagementWebSecurityAutoConfiguration.class from the main class of my spring boot application works for me.

  import org.springframework.boot.SpringApplication;
  import org.springframework.boot.actuate.autoconfigure.security.servlet.ManagementWebSecurityAutoConfiguration;
  import org.springframework.boot.autoconfigure.SpringBootApplication;
  import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;

  @SpringBootApplication(exclude = {SecurityAutoConfiguration.class, 
   ManagementWebSecurityAutoConfiguration.class})
  public class MainApplication implements WebMvcConfigurer {
  public static void main(String[] args) {
  SpringApplication.run(MainApplication.class, args);
    }
   }

Upvotes: 0

Xavier Angeles
Xavier Angeles

Reputation: 21

You need put after the .hasAnyRole and put the user's role after put these you need put .anyRequest().authenticated() and that is all

Upvotes: 2

fish
fish

Reputation: 11

done

Cross-site request forgery



    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.formLogin()
                .loginPage("/test.html").permitAll()
                .loginProcessingUrl("/user/login")
                .and()
                .authorizeRequests()
                .antMatchers("/test.html").permitAll()
                .anyRequest()
                .authenticated()
                .and()          +
                .csrf()         +
                .disable();     +

    }

Upvotes: 0

Related Questions