【问题标题】:Why doesnt my Spring Security authentication work?为什么我的 Spring Security 身份验证不起作用?
【发布时间】:2014-01-28 20:23:23
【问题描述】:

我在/src/main/java/com/dog/bootstrap中放置了以下配置:

@EnableWebSecurity
@Configuration
public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        System.out.println("hello");
        auth.inMemoryAuthentication()
            .withUser("user")
            .password("password")
            .roles("USER");
    }
}

我正在按如下方式加载它:

public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
    // Create the dispatcher servlet's Spring application context
    AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
    dispatcherContext.scan("com.dog.bootstrap");

    // Manage the lifecycle of the root application context
    container.addListener(new ContextLoaderListener(dispatcherContext));

    // Register and map the dispatcher servlet
    ServletRegistration.Dynamic dispatcher =
            container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
    dispatcher.setLoadOnStartup(1);

    Set<String> mappingConflicts = dispatcher.addMapping("/");
    if (!mappingConflicts.isEmpty()) {
        throw new IllegalStateException("'dispatcher' could not be mapped to '/' due " +
                "to an existing mapping.");
    }
}

我的控制器:

@Controller
public class DogController {
    @RequestMapping(value = {"/dog"}, method = RequestMethod.GET)
    @ResponseBody
    public String getSource(@PathVariable("domain") String domain) throws Exception {
        return "dogs";
    }
}

当我启动我的应用程序时,我确实看到 hello 正在打印,所以 configure(AuthenticationManagerBuilder auth) 正在被调用。但是,我的所有端点都不需要我输入登录页面。当我转到localhost:8080/dog 时,它会输出dogs 而不会要求我进行身份验证。

【问题讨论】:

    标签: java spring authentication spring-security restful-authentication


    【解决方案1】:

    您实际上并没有包括过滤器链as described in the last step of this guide。尝试添加这个默认初始化器,它映射到/*

    @Component public class SecurityWebApplicationInitializer 
        extends AbstractSecurityWebApplicationInitializer {
    }
    

    【讨论】:

    • 就是这样!谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 2019-07-18
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2021-04-06
    • 2015-08-27
    • 1970-01-01
    相关资源
    最近更新 更多