在SpringBoot

时间:2017-03-11 14:00:43

标签: spring-mvc authentication spring-boot encoding spring-security

我使用Spring Initializer,嵌入式Tomcat,Thymeleaf模板引擎和包作为可执行的JAR文件生成了一个Spring Boot Web应用程序。

使用的技术:

Spring Boot 1.4.2.RELEASE,Spring 4.3.4.RELEASE,Thymeleaf 2.1.5.RELEASE,Tomcat Embed 8.5.6,Maven 3,Java 8

这是我的安全配置类:

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

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

    @Bean
    public StandardPasswordEncoder encoder() {
        return new StandardPasswordEncoder();
    }

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

        http
            .formLogin()
                .loginPage(loginPage)
                .permitAll()
                .loginProcessingUrl("/tdk/login")
                .failureUrl("/tdk/login?error=true")
                .defaultSuccessUrl("/events/list")
                .and()
            .exceptionHandling()
                .accessDeniedPage("/denied")
                .and()
            .authorizeRequests()
                .antMatchers("/resources/**").permitAll()
                .antMatchers("/mockup/**").permitAll()
                .antMatchers("/users/**").permitAll()
                .antMatchers("/books/**").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("c1f02fa50809b7f715576198eda6466cd17f63404ae6eded7c22290b025baf3868bc8f785267d4ae").roles("ADMIN").and()
                .withUser("test2").password("test2").roles("USER").and()
                .withUser("test3").password("test3").roles("SUPERADMIN");
    }


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

这是我正常运行的Junit测试

public class StandardPasswordEncoderTests {

    @Test
    public void getPasswordForTest1() {
        StandardPasswordEncoder encoder = new StandardPasswordEncoder();
        String password = "test1";

        assertTrue(
                encoder.matches(password, "c1f02fa50809b7f715576198eda6466cd17f63404ae6eded7c22290b025baf3868bc8f785267d4ae"));

    }
}

这是我的登录模板

<form th:action="@{/tdk/login}" method="post">

            <p th:if="${param.error}">
                Bad Credentials  ${param.error}
            </p>

                <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>                      
                <input type="submit" value="LOGIN" />
             </form>

但无论我说什么:

test1 / c1f02fa50809b7f715576198eda6466cd17f63404ae6eded7c22290b025baf3868bc8f785267d4ae

test2 / test2 

我在模板的输出中看到了消息Bad Credentials ${param.error}

1 个答案:

答案 0 :(得分:1)

登录页面中用户名和密码的参数名称与Spring Security配置中的名称不匹配。

您可以更改Spring Security配置以使用登录页面中的参数名称。或者您可以更改登录页面以使用默认参数名称。

请参阅FormLoginConfigurer#usernameParameter

  

执行身份验证时查找用户名的HTTP参数。默认为“用户名”。

FormLoginConfigurer#passwordParameter

  

执行身份验证时查找密码的HTTP参数。默认为“密码”。

您修改的登录页面(使用默认参数名称):

<form th:action="@{/tdk/login}" method="post">
    <p th:if="${param.error}">
        Bad Credentials  ${param.error}
    </p>

    <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="username" placeholder="User" />
    </div>
    <div class="input_label">
         <i class="fa fa-key"></i>
         <input type="password" name="password" placeholder="Password" />
    </div>                      
    <input type="submit" value="LOGIN" />
</form>
相关问题