Spring Boot尝试呈现登录处理网址

时间:2019-03-10 10:48:08

标签: java spring spring-boot spring-security thymeleaf

大家好,我试图用Spring Boot 2和thymeleaf 3创建自定义登录表单,该应用程序有2个安全配置,一个用于移动应用程序用户,另一个用于餐馆,这是一个Web应用程序。我正在为餐馆端实现自定义登录表单,但是提交登录表单时,spring尝试查找localhost:8080 / {loginprocessingurl},这将导致404错误。 这是我的表单,控制器和安全性配置,如下所示:

@Configuration
    @Order(2)   // no order means order of config value is last
    public static class RestaurantSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        protected void configure(HttpSecurity http) throws Exception{
            http.csrf().and()
            .antMatcher("/restaurant/**")  //antmatcher tekil şekilde urlleri farklı configler için gruplamada kullanılır
            .authorizeRequests()
                .antMatchers("/assets/**", "/webjars/**","/static/**").permitAll()
                .anyRequest().hasRole("RESTAURANT")
                .and()
            .formLogin()
                .loginPage("/login")
                .loginProcessingUrl("/restaurant/reslogin")
                .failureUrl("/restaurant/login?error")
                .defaultSuccessUrl("/restaurant/tables",true)
                .permitAll()
                .and()
            .logout()
            .logoutUrl("/restaurant/logout")
            .deleteCookies("JSESSIONID")
            ;
        }

控制器:

@Controller
@RequestMapping(value="/restaurant")
public class RestaurantController {

    @RequestMapping(value="/login",method=RequestMethod.GET)
    public ModelAndView getlogin(Model model) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("login");
        return mav;
    }

和我的登录表格:

  <form class="mt-5" name="f" th:action="@{/restaurant/reslogin}" method="post">
              <!-- <div th:if="${loginError}">
                    <div class="alert alert-danger">
                        Invalid username or password.
                    </div>
                </div> -->
              <div class="form-group">
                <label for="username">Restoran Kullanıcı Adı</label>
                <input class="form-control" id="username" name="username" placeholder="Kullanıcı Adı">
              </div>
              <div class="form-group">
                <label for="password">Parola</label>
                <input type="password"  class="form-control" id="password" name="password"  placeholder="Parola">
              </div>

            <button type="submit" class="btn btn-primary">Giriş</button>

            </form>

当我查看源页面时,我看到百里香没有注入csrf令牌输入。 我使用thymeleaf Extras springsecurity版本3.0.4,如果ı以

的形式手写csrf输入
<input
  type="hidden"
  th:name="${_csrf.parameterName}"
  th:value="${_csrf.token}" />

,它向我产生此错误:EL1007E:在null上找不到属性或字段'parameterName'
   事情是我知道spring应该在背面处理登录处理url并进行身份验证(我正确地知道了UserDetailsS​​ervice的实现并重写了WebSecurityConfigurationAdapter的configureGlobal方法,因为移动应用程序请求运行良好(另一个@Configuration类))但改为重定向成功URL到我,它尝试呈现该登录处理URL。
   另一件事,当我不使用自定义登录处理url而不是使用默认的/ login来发布登录表单时,它说不允许使用405方法,但是我在网络上尝试过的所有其他示例都可以像这样,甚至在Beginning Spring Boot中也可以2本书由K.Siva Prasad Reddy撰写,我以表格形式登录。就像我上面说的那样写,就像魅力!每个依赖项版本都相同!
   我已经处理了这两天,并在所有地方都进行了搜索,但是找不到解决方案,因此所有想法和建议都将受到赞赏。

1 个答案:

答案 0 :(得分:0)

我解决了我的问题,这是由于其他配置类中没有相同的RequestMatcher引起的,

http
    .antMatcher("/user/**")

因此,如果其他春季靴爱好者遇到相同的问题,请确保已将2配置映射为单独的URL。