java – Spring REST安全性 – 以不同方式保护不同的URL
发布时间:2020-12-15 02:53:54 所属栏目:Java 来源:网络整理
导读:我在 Spring 4下使用基本身份验证工作REST API.这些REST服务位于/ api / v1 / ** URL下.但是,我想在不同的url / api / v2 / **下添加另一组REST端点,但使用基于令牌的身份验证进行保护. 是否可以使用一个servlet执行此操作?如何配置Spring Security以对不同
我在
Spring 4下使用基本身份验证工作REST API.这些REST服务位于/ api / v1 / ** URL下.但是,我想在不同的url / api / v2 / **下添加另一组REST端点,但使用基于令牌的身份验证进行保护.
是否可以使用一个servlet执行此操作?如何配置Spring Security以对不同的URL使用不同形式的身份验证? 谢谢. 解决方法
这是
Java配置中的代码示例,它使用UserDetailsS??ervice,并为不同的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... */ } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |