实现UserDetailsService达成用户名登录和短信登录2

来源:4-12 短信登录配置及重构

mjka5632

2018-04-02

@Configuration
public class BrowserSecurityConfig extends AbstractChannelSecurityConfig {
    @Autowired
    private SecurityProperties securityProperties;
    //IDE错误,此处忽略
    @Autowired
    private DataSource dataSource;
 
    @Autowired
    @Qualifier("myUserDetailsService")
    private UserDetailsService userDetailsService;
 
    @Autowired
    private SmsCodeAuthenticationSecurityConfig smsCodeAuthenticationSecurityConfig;
 
    @Autowired
    private ValidateCodeSecurityConfig validateCodeSecurityConfig;
 
 
    /**
     *
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
 
        applyPassowrdAuthenticationConfig(http);
        //导入配置
        http.apply(validateCodeSecurityConfig)
                .and()
        //--------------身份认证
                .apply(smsCodeAuthenticationSecurityConfig)
 
                .and()
                //记住我功能
                .rememberMe()
                //设置数据库Token
                .tokenRepository(persistentTokenRepository())
                //设置时效
                .tokenValiditySeconds(securityProperties.getBrowser().getRememberMeSeconds())
                //拿到User信息
                .userDetailsService(userDetailsService)
                //basic方式
//          http.httpBasic()
                .and()
                //--------对请求做授权(下面都是对授权的配置)
                .authorizeRequests()
                //这个url无需认证
                .antMatchers(SecurityConstants.DEFAULT_LOGIN_PROCESSING_URL_FORM,
                        SecurityConstants.DEFAULT_UNAUTHENTICATION_URL,
                        securityProperties.getBrowser().getLoginPage(),
                        SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX+"/*")
                .permitAll()
                //任何请求
                .anyRequest()
 
                //都需要做身份认证
                .authenticated()
                .and()
                //跨站请求防护
                .csrf().disable()
 
        ;
    }

老师这样不对吗?不知道为什那个回复不行啦,点成采纳答案啦,看你给我的应该是最终版的截图把,我用@Qualifier("myUserDetailsService")有问题?

写回答

1回答

JoJo

2018-04-03

你这只是把myUserDetailsService注入进来了,但是并没有显式的配置AuthenticationManager使用这个myUserDetailsService

0
1
mjka5632
非常感谢!
2018-04-11
共1条回复

Spring Security技术栈开发企业级认证与授权

Spring Security技术栈,REST风格开发常见接口,独立开发认证授权模块保证REST服务安全

2662 学习 · 1561 问题

查看课程