我使用带有grails 2.0的spring security core plugin(1.2.7)
假设我有一个使用@Secured注释的方法的控制器.
class ArticleController {
def springSecurityService
@Secured(['ROLE_PREMIUM_USER'])
def listPremium() {
render 'premium content'
}
}
Run Code Online (Sandbox Code Playgroud)
在我的单元测试中,我想测试具有角色'ROLE_PREMIUM_USER'的用户是否可以看到listPremium方法的内容.我怎样才能做到这一点?
我知道它应该从如下开始:
@TestFor(ArticleController)
@Mock([SpringSecurityService])
class ArticleControllerTests {
void testListPremium() {
defineBeans {
springSecurityService(SpringSecurityService)
}
//but how to login the user here in order to see premium content?
controller.listPremium()
assert response.text() == 'premium content'
}
}
Run Code Online (Sandbox Code Playgroud)
我不知道如何验证检查ROLE_PREMIUM_USER的用户或模拟操作.有帮助吗?
我使用Jersey 2.5.1作为jax-rs实现,我使用Moxy作为JSON序列化器.我将Jersey配置为打印验证错误以在web.xml中输出.
<init-param>
<param-name>jersey.config.beanValidation.enableOutputValidationErrorEntity.server</param-name>
<param-value>true</param-value>
</init-param>
Run Code Online (Sandbox Code Playgroud)
这是正常的,因为验证错误以纯文本(text/plain)返回.问题是我想获得JSON格式的验证错误消息,并且根据Jersey文档为了做到这一点,必须为此配置JSON提供程序.据我所知,当Moxy的依赖关系附加到classpath时,它被配置为JSON提供者.不幸的是,我的验证错误不会以JSON(application/json)格式返回.什么可能是错的.我必须配置额外的位吗?
当我调试ValidationExceptionMapper后面的代码返回带有媒体类型text/plain的Variant对象
if (property != null && Boolean.valueOf(property.toString())) {
final List<Variant> variants = Variant.mediaTypes(
MediaType.TEXT_PLAIN_TYPE,
MediaType.TEXT_HTML_TYPE,
MediaType.APPLICATION_XML_TYPE,
MediaType.APPLICATION_JSON_TYPE).build();
final Variant variant = request.get().selectVariant(variants);
if (variant != null) {
response.type(variant.getMediaType());
} else {
// default media type which will be used only when none media type from {@value variants} is in accept
// header of original request.
// could be settable by configuration property.
response.type(MediaType.TEXT_PLAIN_TYPE);
}
response.entity(
new GenericEntity<List<ValidationError>>(
ValidationHelper.constraintViolationToValidationErrors(cve),
new GenericType<List<ValidationError>>() {}.getType() …Run Code Online (Sandbox Code Playgroud) 我正在开发一个连接到我的服务器的Android应用程序.像往常一样,我的服务器在开发期间是在笔记本电脑上,它的ip正在改变 我想检测我的开发机器的IP地址,即
InetAddress.getLocalHost().getCanonicalHostName()
Run Code Online (Sandbox Code Playgroud)
...并将其注入android strings.xml文件,以便我的Android应用程序在开发过程中连接到正确的ip.
我正在努力确定从哪里获取信息运行什么类型的buildType以后每次都可以执行:
android {
buildTypes.each { buildType ->
if(buildType.name == 'debug') {
def host = InetAddress.getLocalHost().getCanonicalHostName()
}
}
}
Run Code Online (Sandbox Code Playgroud)
假设现在确定该主机.但是我仍然不确定如何替换strings.xml文件中的host以连接到正确的主机.我无法调用processResources,因为抛出以下异常:
Could not find method processResources() for argument [build_11onjivreh0vsff0acv5skf836$_run_closure3@cbbe2cf] on project
Run Code Online (Sandbox Code Playgroud)
任何建议或源代码将不胜感激.