Spring Securityは私の機能を覚えることを実現します.

14813 ワード

一、ページ
登録ページに オプションが追加され、 の選択ボックスはinput checkboxタイプの多選択ボックスでなければならず、そのnameはname="remember-me"でなければならない.

<html>
<head>
<meta charset="UTF-8">
<title>  title>
head>
<body>
    <h3>    h3>
    <form action="/authentication/form" method="post">
        <table>
            <tr>
                <td>   :td> 
                <td><input type="text" name="username">td>
            tr>
            <tr>
                <td>  :td>
                <td><input type="password" name="password">td>
            tr>
            <tr>
                <td>     :td>
                <td>
                    <input type="text" name="imageCode">
                    <img src="/code/image">
                td>
            tr>
            <tr>
                <td colspan='2'>
                    <span style="display: none;">name remember-me     span>
                    <input name="remember-me" type="checkbox" value="true" />
                    <span>   span>
                td>
            tr>
            <tr>
                <td colspan="2"><button type="submit">  button>td>
            tr>
        table>
    form>
body>
html>
二、config配置
security授権された配置クラスにremembergMe構成を追加する:
package com.xh.security.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;

import com.xh.security.validate.code.ValidateCodeFilter;

@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public PasswordEncoder passwordEncoder () {
        return new BCryptPasswordEncoder();
    }

    @Autowired
    private AuthenticationSuccessHandler myAuthenticationSuccessHandler;

    @Autowired
    private AuthenticationFailureHandler myAuthenctiationFailureHandler;

    @Autowired
    private DataSource dataSource;

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public PersistentTokenRepository persistentTokenRepository () {
        JdbcTokenRepositoryImpl tokenRepositoryImpl = new JdbcTokenRepositoryImpl();
        tokenRepositoryImpl.setDataSource(dataSource);
        //                    ,    true,     
//      tokenRepositoryImpl.setCreateTableOnStartup(true);
        return tokenRepositoryImpl;
    }

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

        ValidateCodeFilter validateCodeFilter = new ValidateCodeFilter();
        validateCodeFilter.setAuthenctiationFailureHandler(myAuthenctiationFailureHandler);

        http.addFilterBefore(validateCodeFilter, UsernamePasswordAuthenticationFilter.class)
            .formLogin()//            
                .loginPage("/authentication/require")//        
                .loginProcessingUrl("/authentication/form")//        
                .successHandler(myAuthenticationSuccessHandler)
                .failureHandler(myAuthenctiationFailureHandler)
                .and()
            .rememberMe()
                .tokenRepository(persistentTokenRepository())
                //     
                .tokenValiditySeconds(3600)
                .userDetailsService(userDetailsService)
            .and()
            .authorizeRequests()//      
            // error  127.0.0.1            
            .antMatchers("/myLogin.html", "/authentication/require",
                    "/authentication/form","/code/image").permitAll()//            ,        
            .anyRequest() //     
            .authenticated()//; //        
            .and()
            .csrf().disable();//       
    }

}