Nothing prevents you from using Authorization header in a creative way, i.e., by embedding the Android ID into it. Then, in order to add authentication to your endpoints, you can use an AOP interceptor:
Protected operation marker interface:
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ProtectedOperation {
}
Interceptor:
@Aspect
@Component
public class SecurityAspect {
private CorporateService corpService; // this is your custom service to check Android IDs
@Autowired
public SecurityAspect(CorporateService corpService) {
this.corpService = corpService;
}
@Around("@annotation(operation)")
public Object protectedOperationPermissionCheck(final ProceedingJoinPoint pjp, final ProtectedOperation operation) throws Throwable {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
String header = requestAttributes.getRequest().getHeader("Authorization");
String androidId = // get the ID from header - try not to use existing authorization header formats like Bearer, Negotiate etc. to avoid collision with other authentication systems
if (corpService.isAuthorized(androidId)) {
return pjp.proceed();
}
response.setStatus(HttpServletResponse.SC_FORBIDDEN);
response.flushBuffer();
return null;
}
}
Make sure to add the spring-boot-starter-aop
dependency to your pom.xml, for @Aspect
support
EDIT: to protect an endpoint, annotate the endpoint method in your controller with @ProtectedOperation
, and add @EnableAspectJAutoProxy
to your Spring Boot application
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…