Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/402.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java SpringREST安全性-以不同的方式保护不同的URL_Java_Spring_Rest_Spring Mvc_Spring Security - Fatal编程技术网

Java SpringREST安全性-以不同的方式保护不同的URL

Java SpringREST安全性-以不同的方式保护不同的URL,java,spring,rest,spring-mvc,spring-security,Java,Spring,Rest,Spring Mvc,Spring Security,我在Spring4下使用基本身份验证实现了RESTAPI。这些REST服务位于/api/v1/**URL下。但是,我想在不同的url/api/v2/**下添加另一组REST端点,但受基于令牌的身份验证保护 可以用一个servlet来实现这一点吗?如何配置Spring安全性以对不同URL使用不同形式的身份验证 谢谢。以下是Java配置中的一个代码示例,它使用了UserDetailsService,并为不同的URL端点提供了不同的安全配置: @Configuration @EnableWebMvcS

我在Spring4下使用基本身份验证实现了RESTAPI。这些REST服务位于/api/v1/**URL下。但是,我想在不同的url/api/v2/**下添加另一组REST端点,但受基于令牌的身份验证保护

可以用一个servlet来实现这一点吗?如何配置Spring安全性以对不同URL使用不同形式的身份验证


谢谢。

以下是Java配置中的一个代码示例,它使用了
UserDetailsService
,并为不同的URL端点提供了不同的安全配置:

@Configuration
@EnableWebMvcSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDetailsService userDetailsService;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Configuration
    @Order(1)
    public static class ApiWebSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .antMatcher("/api/v1/**")
                    .httpBasic()
                        .realmName("API")
                        .and()
                    .csrf().disable()
                    .authorizeRequests()
                    .antMatchers("/api/v1/**").authenticated();
        }
    }

    @Configuration
    @Order(2)
    public static class ApiTokenSecurityConfig extends WebSecurityConfigurerAdapter{

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                    .antMatcher("/api/v2/**")
                    /* other config options go here... */
        }

    }
}

您应该能够在spring security applicationContext中有不止一个入口点,您可以在其中指定截取url模式。您是说为每个url模式提供更多受保护的void configure(HttpSecurity http)方法吗?你能给我举个例子说明你的意思吗?谢谢。我只使用过基于xml的spring security设置,据我所知,可以使用不同的入口点和自己的一组截取URL设置多个标记。如果你愿意,我可以试着把一些基本的东西组合起来。当然,我愿意。没关系,我可能可以将其转换为编程配置。谢谢。请检查此链接,您可以为您的应用程序配置多个入口点。我的使用案例略有不同。是否可以将
/api/**
配置为使用基本身份验证,将其余应用程序配置为使用表单登录?可以。在api安全配置的第一行中使用
http.antMatcher(“/api/**”)
,在应用程序的其余部分的配置中使用
http.requestMatcher(new NegatedRequestMatcher(new AntPathRequestMatcher(“/api/**”))
。如果我想为每个端点使用不同的UserDetails服务,该怎么办?