Java 如何使用Spring安全性解除方法的安全性
我已经为一个RESTfulWeb服务项目实现了Spring安全性。它具有具有相同url模式但具有不同请求方法类型的请求映射Java 如何使用Spring安全性解除方法的安全性,java,spring,spring-mvc,spring-security,spring-annotations,Java,Spring,Spring Mvc,Spring Security,Spring Annotations,我已经为一个RESTfulWeb服务项目实现了Spring安全性。它具有具有相同url模式但具有不同请求方法类型的请求映射 @RequestMapping(value = "/charity/accounts", method = RequestMethod.POST) public AccountResponseDto createAccount(HttpServletResponse response, @RequestBody AccountRequestDto requestDTO) {
@RequestMapping(value = "/charity/accounts", method = RequestMethod.POST)
public AccountResponseDto createAccount(HttpServletResponse response, @RequestBody AccountRequestDto requestDTO) {
// some logics here
}
@RequestMapping(value = "/charity/accounts", method = RequestMethod.GET)
public AccountResponseDto getAccount(HttpServletResponse response) {
// some logics here
}
@RequestMapping(value = "/charity/accounts", method = RequestMethod.PUT)
public void updateAccount(HttpServletResponse response, @RequestBody AccountRequestDto requestDTO){
// some logics here
}
目前所有这些方法都需要授权才能执行,但我需要删除createAccount(…)
method的授权。是否有基于注释的解决方案
注意:我需要一个不会影响url模式更改的解决方案,因为它会影响许多其他模块 下面是一个示例配置,它允许请求
注册
和关于
:
@EnableWebSecurity
@Configuration
public class CustomWebSecurityConfigurerAdapter extends
WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
auth
.inMemoryAuthentication()
.withUser("user") // #1
.password("password")
.roles("USER")
.and()
.withUser("admin") // #2
.password("password")
.roles("ADMIN","USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeUrls()
.antMatchers("/signup","/about").permitAll();
}
}
有关详细信息,请参阅
对控制器的建议。如果所有以/charity
为前缀的请求都将由CharityController处理,则可以通过以下方式映射请求:
@Controller
@RequestMapping(value="/charity")
class CharityController {
@RequestMapping(value = "/accounts", method = RequestMethod.GET)
public AccountResponseDto getAccount(HttpServletResponse response){
}
}
更新
以下内容应该适合您
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(HttpMethod.POST, new String [] {"/charity/accounts", "/charity/people"}).permitAll();
}
下面是一个示例配置,允许请求
注册
和关于
:
@EnableWebSecurity
@Configuration
public class CustomWebSecurityConfigurerAdapter extends
WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) {
auth
.inMemoryAuthentication()
.withUser("user") // #1
.password("password")
.roles("USER")
.and()
.withUser("admin") // #2
.password("password")
.roles("ADMIN","USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeUrls()
.antMatchers("/signup","/about").permitAll();
}
}
有关详细信息,请参阅
对控制器的建议。如果所有以/charity
为前缀的请求都将由CharityController处理,则可以通过以下方式映射请求:
@Controller
@RequestMapping(value="/charity")
class CharityController {
@RequestMapping(value = "/accounts", method = RequestMethod.GET)
public AccountResponseDto getAccount(HttpServletResponse response){
}
}
更新
以下内容应该适合您
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers(HttpMethod.POST, new String [] {"/charity/accounts", "/charity/people"}).permitAll();
}
这就是为什么我们有角色和授权。首先,我们可以定义谁可以获取/放置/发布并相应地向用户授予权限 然后我们可以在GET/PUT/POST方法上相应地注释为@Secured(“ROLE_ADMIN”)
要获得不安全的权限,您可以添加@PreAuthorize(“isAnonymous()”)或@Secured(“MY_CUSTOM_Anonymous_ROLE”)这就是为什么我们有角色、授权。首先,我们可以定义谁可以获得/放置/发布并相应地向用户授予权限 然后我们可以在GET/PUT/POST方法上相应地注释为@Secured(“ROLE_ADMIN”)
为了不安全地获取,您可以添加@PreAuthorize(“isAnonymous()”)或@Secured(“MY_CUSTOM_Anonymous_ROLE”)感谢James的快速回复,但在我的情况下,我不能使用“http.authorizeUrls().antMatchers(“/charity/accounts”).permitAll()”,因为目前其他两种方法也使用了相同的url模式(getAccount和updateAccount方法),我需要授权。我只需要从CreateCount方法而不是其他方法中删除授权。@zulox,很抱歉我没有注意到url模式。请查找更新的答案部分。感谢James的快速回复,但在我的情况下,我不能使用“http.authorizeUrls().antMatchers(”/charity/accounts“.permitAll();”,因为当前其他两个方法(getAccount和updateAccount方法)也使用了相同的url模式,我需要授权。我只需要从CreateCount方法而不是其他方法中删除授权。@zulox,很抱歉我没有注意到url模式。请找到更新的答案部分。发布您的安全配置!!发布您的安全配置!!关于
@PreAuthorize(“isAnonymous()”)的有趣提示
。它还允许在用@securited(“…”)
注释的类中取消方法的安全性,以防您只需要取消整个类中的一个方法的安全性。有关@PreAuthorize(“isAnonymous()”)
的有趣提示。它还允许在用@securited(“…”)注释的类中取消方法的安全性
以防您只需要在整个类中取消保护一个方法。