spring boot 不通过controller访问templates下的html文件

在学习spring boot security 时,遇到一个问题,在做权限控制时,想在访问接口时,都跳转到自定义的login.html页面。但是像这样写实现不了。究其原因,是因为把html放在了templates下面,而在springboot 中,templates中的文件是需要通过视图解析器才能去方问的。

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

方法一:把login.html文 件放到static下。

方法二:新建WebMvcConfig类

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/login").setViewName("login");
    }
}

把上面的登录页面改为

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

这样就可以正常跳转了。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值