【问题标题】:Thymeleaf + Spring Security integrationThymeleaf + Spring Security 集成
【发布时间】:2017-07-25 13:02:09
【问题描述】:

我已经使用 Spring Initializr 生成了一个 Spring Boot Web 应用程序,使用嵌入式 Tomcat + Thymeleaf 模板引擎,并打包为一个可执行的 JAR 文件。

使用的技术:

Spring Boot 1.4.2.RELEASE、Spring 4.3.4.RELEASE、Thymeleaf 2.1.5.RELEASE、Tomcat 嵌入 8.5.6、Maven 3、Java 8

这是我的安全配置类:

@Configuration
@PropertySource("classpath:/com/tdk/iot/config/app-${APP-KEY}.properties")
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${securityConfig.formLogin.loginPage}")
    private String loginPage;

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

        http
            .formLogin()
                .loginPage(loginPage)
                .defaultSuccessUrl("/books/list")
                .permitAll()
                .and()
            .exceptionHandling()
                .accessDeniedPage("/denied")
                .and()
            .authorizeRequests()
                .antMatchers("/mockup/**").permitAll()
                .antMatchers("/dinners/**").permitAll()
                .antMatchers("/welcome/**").authenticated()
                .and()
            .logout()
                .permitAll()
                .logoutSuccessUrl("/index.html");
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .inMemoryAuthentication()
                .passwordEncoder(new StandardPasswordEncoder())
                .withUser("test1").password("test1").roles("ADMIN").and()
                .withUser("test2").password("test2").roles("USER").and()
                .withUser("test3").password("test3").roles("SUPERADMIN");
    }




    @Bean
    public  static PropertySourcesPlaceholderConfigurer propertyDefaultConfig() {
        return new PropertySourcesPlaceholderConfigurer();
    }
}

这是我的登录模板

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
...
</head>
    <div class="wrap">
        <div class="login">
            <div class="logo"><img src="../../../images_pebloc/login.png" width="224" height="71"  /></div>

              <form th:action="@{/login.html}" method="post">
                <p th:if="${loginError}" class="error">Wrong user or password</p>
                    <div class="input_label"><i class="fa fa-user"></i><input type="text" name="user" placeholder="User" /></div>
                    <div class="input_label"><i class="fa fa-key"></i><input type="password" name="pass" placeholder="Password" /></div>

                </form>

              <input type="submit" value="LOGIN" />
            <div class="forget">
                <a href="#">Do you forgot your password?</a><br/>
                <br/>
                <span>tdk</span>
            </div>

        </div>
    </div>

但是当我点击时,没有任何反应,控制台中没有 HTML 错误,没有 javascript 错误,没有表单错误,没有提交,无论我在哪里输入表单

【问题讨论】:

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


    【解决方案1】:

    您的提交按钮不在form 标记内。将其移动到 &lt;/form&gt; 内,这应该会触发您的操作。

    【讨论】:

      猜你喜欢
      • 2020-08-05
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 2018-02-23
      • 2015-11-08
      • 2021-02-23
      • 2021-03-25
      • 2012-01-18
      相关资源
      最近更新 更多