【发布时间】:2014-09-10 14:07:47
【问题描述】:
我正在尝试让spring-boot-starter-security 与spring-boot-starter-web 和spring-boot-starter-tomcat 一起工作。我尝试按照spring-boot-sample-secure 和spring-boot-sample-web-secure 的指南进行操作,但没有成功。
我正在尝试构建一个没有任何 ui 交互的 REST 应用程序。因此,我发现这两个样本都不完全适合我的目的。目前我的解决方案是使用 AOP。
@Before("execution(* my.zin.rashidi.openshift.tomcat.controller.*.*(..)) && args(authorization, ..)")
public void authenticate(String authorization) {
if (!isEmpty(authorization)) {
SecurityContextHolder.getContext().setAuthentication(
new UsernamePasswordAuthenticationToken("user", "N/A",
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"))
);
}
}
@RequestMapping(method = GET)
public ResponseEntity<User> get(@RequestHeader String authorization, @RequestBody User user) {
HttpStatus status = OK;
User returnObject = null;
try {
returnObject = service.get(user);
} catch (AuthenticationCredentialsNotFoundException e) {
status = UNAUTHORIZED;
}
return new ResponseEntity<User>(returnObject, status);
}
解决方案对我有用。但是我想知道这是否是一个好的解决方案。我很好奇是否有更好的解决方案。
在此先感谢您的帮助!
【问题讨论】:
-
目前还不清楚你用它实现了什么。它是身份验证端点吗?为什么需要 AOP?客户端是否必须发送 cookie 来验证其他请求,所以您使用 Spring Security 来管理这些请求?
-
嗨戴夫·赛尔。客户端将在每个请求的标头中发送“授权”。 AOP 将验证授权,在这种情况下只是确保它不为空,然后再继续处理请求。我正在使用 Spring Security 来存储用户的角色并在服务类中使用 @Secured。
-
另外关于我想要实现的目标。我正在尝试做一些类似于 spring-boot-secure-sample 但用于 web 的事情。没有 UI 交互,每次请求都会对客户端进行身份验证。
-
我不清楚如何对与
UserController不同的控制器的请求进行身份验证。UserController本身甚至有一种方法与切入点不匹配。那么任何人都可以在您的系统中创建新用户? -
我还没有创建其他控制器,但我的计划是将它们存储在同一个包中,它们的参数也将以
authorization开头。你说得对。任何人都可以创建用户。
标签: java tomcat spring-security spring-boot