繁体   English   中英

无法获得Spring Security来拦截请求

[英]having trouble getting spring security to intercept requests

在四处搜索并查看许多其他示例之后,我似乎无法让Spring拦截请求。 我不能正确配置某些东西,否则我的期望会被误导。 有人可以告诉我我在做什么错吗?

这将设置注释配置

public class WebMVCApplicationInitializer implements WebApplicationInitializer
{
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException
    {
        WebApplicationContext context = getContext();
        servletContext.addListener(new ContextLoaderListener(context));
        ServletRegistration.Dynamic dispatcher =
                servletContext.addServlet("DispatcherServlet", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/*");
    }

    private AnnotationConfigWebApplicationContext getContext()
    {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation(String.valueOf(this.getClass().getPackage()));
        return context;
    }
}

我的WebMvcConfig-指定要查找带注释的控制器的程序包

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "myproject.controller")
public class WebMvcConfig extends WebMvcConfigurerAdapter
{

}

我的SecurityConfig-应该匹配并要求对所有内容进行身份验证

@Configuration
@EnableWebMvcSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter
{

    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        // @formatter:off
        http
            .authorizeRequests()
                .anyRequest().authenticated();
        // @formatter:on
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
    {
        auth
            .inMemoryAuthentication()
            .withUser("user").password("password").roles("USER");
    }
}

最后,我的控制器-给出了一个简单的测试响应

@RestController
public class SessionController
{
    @ResponseBody
    @RequestMapping(value = "echo", method = RequestMethod.GET)
    public Map<String, Object> testResponse()
    {
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("data", "test for echo");
        return modelMap;
    }
}

现在,我的期望是,当我运行此命令并将浏览器指向/ echo时,它应该要求身份验证,错误或其他提示。 相反,我只是得到{"data":"test for echo"} (我知道我没有指定身份验证提供程序,但至少会期望出现错误。)

我想念什么?

n / m-我知道了。

我需要将安全过滤器链添加到应用程序上下文中。

    FilterRegistration.Dynamic security =
            servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy());
    security.addMappingForUrlPatterns(dispatcherTypes, true, "/*");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM