简体   繁体   English

无法获得Spring Security来拦截请求

[英]having trouble getting spring security to intercept requests

After searching all around and looking at lots of other examples, I can't seem to get Spring to intercept requests. 在四处搜索并查看许多其他示例之后,我似乎无法让Spring拦截请求。 I must not have something configured correctly, or my expectations are misguided. 我不能正确配置某些东西,否则我的期望会被误导。 Could someone tell me what I'm doing wrong? 有人可以告诉我我在做什么错吗?

This sets up the annotation config 这将设置注释配置

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;
    }
}

My WebMvcConfig - specifies which package to find annotated controllers 我的WebMvcConfig-指定要查找带注释的控制器的程序包

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

}

My SecurityConfig - should match and require authentication on everything 我的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");
    }
}

finally, my Controller - just gives a trivial response for testing 最后,我的控制器-给出了一个简单的测试响应

@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;
    }
}

Now, my expectation is that when I run this, and point a browser to /echo, it should require authentication, or error, or something. 现在,我的期望是,当我运行此命令并将浏览器指向/ echo时,它应该要求身份验证,错误或其他提示。 Instead, I simply get {"data":"test for echo"} . 相反,我只是得到{"data":"test for echo"} (I recognize that I haven't specified an authentication provider, but I'd at least expect an error.) (我知道我没有指定身份验证提供程序,但至少会期望出现错误。)

What am I missing? 我想念什么?

n/m - I figured it out. n / m-我知道了。

I needed to add the security filter chain to the application context. 我需要将安全过滤器链添加到应用程序上下文中。

    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