从Spring Security访问被拒绝的html资源

时间:2018-10-16 22:10:58

标签: java spring security

我正在尝试使用Spring Boot和Spring Security创建一个简单的登录名,但我不明白为什么它不起作用。 基本上我有2个视图,分别位于resources / login / login.html和resources / login / registerUser.html中 每当我尝试登录或注册时,都会拒绝访问我:( 我想它无法访问这2种资源,但是我不明白这是怎么回事:(

控制器:

@RequestMapping("/showReg")
    public String showRegistrationPage() {
        return "login/registerUser";
    }

    @RequestMapping(value = "/registerUser", method = RequestMethod.POST)
    public String register(@ModelAttribute("user") User user) {
        user.setPassword(encoder.encode(user.getPassword()));
        userRepository.save(user);
        return "login/login";
    }

    @RequestMapping(value = "/loginForm", method = RequestMethod.POST)
    public String login(@RequestParam("email") String email, @RequestParam("password") String password, Model model) {
        boolean loginResponse = securityService.login(email, password);
        System.out.println(loginResponse);
            if (loginResponse) {
                return "findFlights";
            } else {
                model.addAttribute("msg", "Invalid username or password.Please try again!");
            }
        return "login/login";
    }

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

WebSecurityConfig类

@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/assets/**" ,"/showReg", "/", "/index.html", "/registerUser", "/login", "/showLogin",
                        "/login/*", "/reservations/*")
                .permitAll().antMatchers("/admin/showFlight").hasAnyAuthority("ADMIN").anyRequest().authenticated()
                .and().csrf().disable();
    }

    @Bean(name = BeanIds.AUTHENTICATION_MANAGER)
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

1 个答案:

答案 0 :(得分:1)

使用重载的configure(WebSecurity web)方法。

@Override
public void configure(WebSecurity web) throws Exception {
    web.ignoring()
       .antMatchers("/assets/**" ,"/showReg", "/", "/index.html", "/registerUser", "/login", "/showLogin", "/login/*", "/reservations/*");
}