spring security 사용자 정의 데이터베이스 테이블 구조

3709 단어 필기 하 다.
spring security 로 사용자 에 게 안전 인증 을 할 때 골 치 아 픈 문제 에 부 딪 혔 습 니 다. spring security 는 기본 데이터베이스 시트 구 조 를 사용 합 니 다. 기본 표 는 users 입 니 다. 사실 귀 찮 으 면 이 기본 설정 을 직접 사용 해도 됩 니 다. 그러나 제 프로젝트 에는 표 구조 규범 이 있 습 니 다. 이 럴 때 기본 표 구 조 를 바 꿔 야 합 니 다.다음은 security Config. java 의 코드 입 니 다. jdbcDaoImpl 의 usersByUsernameQuery 와 authorities ByUsernameQuery 의 sql 문 구 를 덮어 서 기본 사용자 표 users 를 변경 합 니 다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

import javax.sql.DataSource;

/**
 *      .
 *
 */
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    /**
     *      
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/css/**", "/js/**", "/static/fonts/**").permitAll() //      
            .antMatchers("/index","/").hasRole("ADMIN")   //            
            .and()
            .formLogin()   //   Form       
            .loginPage("/login").failureUrl("/login-error"); //        
    }

    @Autowired
    private DataSource dataSource;

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().dataSource(dataSource)
                .usersByUsernameQuery("select userid,userpassword,enableflag "
                        +"from ftp_user where userid=?")
                .authoritiesByUsernameQuery("select username, authority "
                        +"from authorities where username=?");
    }
}

여기 dataSource 는 @ Autowired 주 해 를 추가 하여 주입 합 니 다.

좋은 웹페이지 즐겨찾기