小编Mik*_*key的帖子

Spring @Resource Handling

@Resource在使用Spring bean 注释的字段时遇到了问题.是)我有的:

带有setter方法的字段,带注释 @Resource

@Resource
private URL someUrl;

public void setSomeUrl(URL someUrl) {
    this.someUrl = someUrl;
}
Run Code Online (Sandbox Code Playgroud)

<env-entry>我的部署描述符中的标记(web.xml)

<env-entry>
    <env-entry-name>someUrl</env-entry-name>
    <env-entry-type>java.net.URL</env-entry-type>
    <env-entry-value>http://somedomain.net/some/path</env-entry-value>
</env-entry>
Run Code Online (Sandbox Code Playgroud)

应用程序无法以a开头BeanCreationException,我没想到,因为我不一定要Spring注入一个Spring管理的bean.我希望Spring处理@Resource和检索JNDI资源.

这是Spring 2.5.6SEC03,bean本身注释@Service为自动装配到其他@Component实例中.在这种情况下,Servlet容器是Tomcat 7,但最终将部署到Weblogic 10上,所以虽然我希望理想的解决方案同时适用于两者,但Weblogic是必备的.

我是否在Spring 2.5中滥用此功能?一般来说?我有点遗失吗?我误解了JNDI的一些事情?所有帮助表示赞赏.谢谢.

java spring tomcat weblogic jndi

7
推荐指数
1
解决办法
6455
查看次数

Spring Security:使用@PreAuthorize注释不保护方法

我想在我的托管会话bean中保护特定角色的方法 "ROLE_ADMIN"

配置(的applicationContext-security.xml文件):

<global-method-security pre-post-annotations="enabled" jsr250-annotations="enabled" secured-annotations="enabled"/>
    <http auto-config="true" use-expressions="true">
        <intercept-url pattern="/**" access="isAuthenticated()"/>
        <intercept-url pattern="/**" access="permitAll()"/>
        <form-login
         login-processing-url="/j_spring_security_check"
         login-page="/login.jsf"
         default-target-url="/main.jsf"
         authentication-failure-url="/login.jsf" />

    <session-management>
           <concurrency-control max-sessions="1" error-if-maximum-exceeded="false" />
    </session-management>
    </http>


    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <user-service>
                <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" />
                <user name="user1" password="user1" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

    <beans:bean id="loggerListener" class="org.springframework.security.authentication.event.LoggerListener"/>
Run Code Online (Sandbox Code Playgroud)

bean的安全方法:

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public String buy() {
...
    }
Run Code Online (Sandbox Code Playgroud)

当我在网页下登录user1anonym单击"购买"按钮时,它仍然会重定向到下一页.

我希望发生一些访问被拒绝的异常,但事实并非如此.

security spring pre role authorize

5
推荐指数
1
解决办法
8565
查看次数

当通用类型信息不可用时,如何避免编译器警告?

我正在使用Spring RestTemplate来对REST Web服务进行调用.其中一个调用是返回某种类型的对象列表.这些RestTemplate方法要求提供类参数以指示预期的返回类型.

// restTemplate is type org.springframework.web.client.RestTemplate
URI restServiceURI = new URI("http://example.com/foo")
restTemplate.getForObject(restServiceURI, List<Foo>.class);
Run Code Online (Sandbox Code Playgroud)

显然,这不编译..class当您提供类似的类型参数时,您无法获得静态属性.我删除type参数时会编译代码,但会生成rawtypes编译器警告.

我的问题很简单.我是坚持抑制编译器警告还是有更简洁的方法来代码?

java generics reflection generic-collections

5
推荐指数
1
解决办法
1887
查看次数

为什么以及如何通过编译器对这两个条件进行不同的处理?

以下两个代码示例表示相同的逻辑.检查字符串是否为null并根据该检查进行分支.第一个样本安全编译.第二个产生与Java泛型相关的类型不匹配错误.我的问题似乎很简单,但它让我望而却步.为什么编译器对这两个语句的处理方式不同?我怎样才能更好地了解这里发生了什么?

/* compiles cleanly */
protected Collection<String> getUserRoles(Object context,
        Set<String> mappableRoles) {
    String cookieValue = extractCookieValue(context);
    if (cookieValue != null) {
        return securityService.getRolesForUser(cookieValue);
    } else {
        return Collections.emptySet();
    }
}


/* produces a compiler error */
protected Collection<String> getUserRoles(Object context,
            Set<String> mappableRoles) {
    String cookieValue = extractCookieValue(context);
    return cookieValue == null ? Collections.emptySet()
            : securityService.getRolesForUser(cookieValue);
}
Run Code Online (Sandbox Code Playgroud)

来自Eclipse的编译器错误.

Type mismatch: cannot convert from Set<capture#1-of ? extends Object> to Collection<String>

根据要求,这是SecurityService接口的相关部分.

public interface SecurityService {
    public Set<String> getRolesForUser(String userId);
}
Run Code Online (Sandbox Code Playgroud)

java generics type-inference conditional-operator type-bounds

4
推荐指数
2
解决办法
168
查看次数