我正在尝试使用带有注释的Spring Security Java配置。基本上,我想结合使用注释方法和静态配置。
http
.authorizeRequests()
.anyRequest().authenticated()
和
@Controller
@RequestMapping({"/version", "/*/version"})
@PermitAll
public class VersionController {
// Implementation
}
不久,我希望这@PermitAll
必须优先于其他选民。是否可以在@EnableGlobalMethodSecurity(jsr250Enabled = true)
这种情况下使用?
我稍微改变了看法,开始考虑另一种解决方案。如果我在弹簧安全性启动之前就知道我的控制器,则可以通过处理它们来对其进行配置。基本上,我创建了一个处理器,我的实现的简单出价简化版本是:
@Component
public class PermitAllProcessor {
private static final Logger logger = LoggerFactory.getLogger(PermitAllProcessor.class);
@Autowired
AbstractHandlerMethodMapping abstractHandlerMethodMapping;
public String[] process() {
Map<RequestMappingInfo, HandlerMethod> handlerMethods = abstractHandlerMethodMapping.getHandlerMethods();
ArrayList<String> urls = Lists.newArrayList();
for(Map.Entry<RequestMappingInfo, HandlerMethod> entry : handlerMethods.entrySet()) {
PermitAll annotation = getPermitAll(entry.getValue());
if(annotation != null) {
Set<String> patterns = entry.getKey().getPatternsCondition().getPatterns();
urls.addAll(patterns);
}
}
logger.info("Permitted urls as follows: {}",
StringUtils.collectionToDelimitedString(urls, ",", "[", "]"));
return urls.toArray(new String[urls.size()]);
}
private PermitAll getPermitAll(HandlerMethod value) {
PermitAll annotation = value.getMethodAnnotation(PermitAll.class);
if(annotation == null){
annotation = value.getBeanType().getAnnotation(PermitAll.class);
}
return annotation;
}
}
基本上
.authorizeRequests()
.antMatchers(permitAllProcessor.process()).permitAll()
.anyRequest().authenticated();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句