标签: spring-java-config

Spring:通过java配置在Controller层中启用全局方法安全性

我正在尝试将我的xml servlet配置迁移到java配置.

以下配置是我的servlet配置,它在Controller层上启用自定义安全注释.

<security:global-method-security pre-post-annotations="enabled">
    <security:expression-handler ref="expressionHandler"/>
</security:global-method-security>

<bean id="expressionHandler" class="yyy.MyMethodSecurityExpressionHandler" />
Run Code Online (Sandbox Code Playgroud)

我还有一个工作的spring security xml配置,这是为了被java配置替换,但现在不是.这里有一些我的安全配置:

<bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
        <property name="userDetailsService" ref="userDetailsService" />
    </bean>

<bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
    <constructor-arg>
         <ref bean="authenticationProvider"/>
    </constructor-arg>
</bean>

<security:authentication-manager>
    <security:authentication-provider user-service-ref="userDetailsService" />
</security:authentication-manager>

<security:global-method-security pre-post-annotations="enabled" />
Run Code Online (Sandbox Code Playgroud)

我想开始迁移我的servlet配置@PreAuthorize,@PostAuthorize在Controller层中启用安全性和标记.

我发现了这个注释:@EnableGlobalMethodSecurity(prePostEnabled=true)但是把它放在我的servlet配置上:

@Configuration
@ComponentScan(basePackages= {
        "....."         
})
@EnableGlobalMethodSecurity(prePostEnabled=true)

public class WebappServletConfig extends WebMvcConfigurationSupport {
Run Code Online (Sandbox Code Playgroud)

我得到这个例外:

java.lang.IllegalArgumentException: Expecting to only find a single bean for type interface org.springframework.security.authentication.AuthenticationManager, but found []
Run Code Online (Sandbox Code Playgroud)

而且我不知道如何设置我的自定义表达式处理程序!

有人提示吗?谢谢

java spring spring-mvc spring-security spring-java-config

5
推荐指数
2
解决办法
2万
查看次数

Spring引导Thymeleaf片段子目录视图错误与jar

我的Spring Boot应用程序在Eclipse中运行良好,在命令行中运行gradle.但是从java -jar启动时无法从子目录加载片段....

使用默认的Spring Boot和Thymeleaf设置和gradle.

文件夹结构

src/main/resources/
---templates/
      ---homepages/
           ---homepage
           ---head
Run Code Online (Sandbox Code Playgroud)

为Thymeleaf尝试了明确的视图解析器.没运气.

片段导致问题.

<head th:include="/homepages/head"></head>
Run Code Online (Sandbox Code Playgroud)

从jar启动时出错

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.
Fri May 16 18:54:44 EDT 2014
There was an unexpected error (type=Internal Server Error, status=500).
Error resolving template "/homepages/head", template might not exist or might not be accessible by any of the configured Template Resolvers (homepages/homepage:5)
Run Code Online (Sandbox Code Playgroud)

使用Spring Boot的默认设置.

buildscript {
    repositories {
        maven { url …
Run Code Online (Sandbox Code Playgroud)

jar thymeleaf build.gradle spring-boot spring-java-config

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

Spring 4, Hibernate JPA JarInputStreamBasedArchiveDescriptor Error on Websphere 8.5.5

I have a Spring MVC/Spring-Data-JPA (with Hibernate) application running perfectly on the Websphere 8.5.5 Liberty Profile. However, when I deploy to the full version of Websphere 8.5.5. I get the error pasted below.

My other beans naturally rely on the EntityManagerFactory, so this kills me. Have been googling all day to no avail.

  • Environment: Websphere 8.5.5
  • Spring Version: 4.0.4.RELEASE
  • Hibernate EntityManager Version: 4.3.5
  • IDE: IBM RAD (Version: 8.5.5) (Eclipse 3.6.3)

STACK TRACE:

[5/20/14 19:29:14:800 EDT] …
Run Code Online (Sandbox Code Playgroud)

spring hibernate hibernate-entitymanager websphere-8 spring-java-config

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

使用Java Config时如何防止Spring生命周期方法?

在返回对象后,如何防止问题服务器上的@PostConstruct方法被Spring调用?

@Configuration
class MyConfig {
    @Bean
    public ProblematicService problematicService() {
        ProblematicService service = someMethodOutsideMyControl();
        // ProblematicService is constructed for me by other code (outside of Spring)
        // and it happens to have a @PostConstruct method. The @PostConstruct method
        // cannot be invoked here or by Spring once this method returns.
        return service;
    }
}
Run Code Online (Sandbox Code Playgroud)

我相信将结果包装在FactoryBean中会产生预期的效果,但我需要在几个地方重复这段代码,所以我正在寻找更优雅的解决方案.

java spring spring-java-config

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

注释@EnableSpringDataWebSupport不适用于WebMvcConfigurationSupport吗?

我已经使用WebMvcConfigurerAdapter已有一段时间了。由于无法使用getInterceptors()方法获取所有已注册的拦截器,因此我切换到了WebMvcConfigurationSupport,它具有许多默认的已注册Spring Bean,例如ContentNegotiationManager,ExceptionHandlerExceptionResolverusw。

现在,我已经意识到,尽管我在WebConfig类上使用了@EnableSpringDataWebSupport批注,但非常方便的DomainClassConverter(使用CrudRepository将域类ID转换为域类对象)并未默认注册。

当我像这样显式定义此bean时,它就会起作用。

@EnableSpringDataWebSupport
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
    @Bean
    public DomainClassConverter<?> domainClassConverter() {
        return new DomainClassConverter<FormattingConversionService>(mvcConversionService());
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,为什么EnableSpringDataWebSupport无法与WebMvcConfigurationSupport一起使用?

spring spring-mvc spring-java-config spring-data-commons

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

Spring MessageSource似乎忽略了属性fallbackToSystemLocale

我在配置Spring MessageSource以忽略我的系统区域设置时遇到问题.当我使用null locale参数调用getMessage时,我希望我的MessageSource选择默认属性文件messages.properties.相反,它选择messages_en.properties.当我将此属性文件的名称更改为messages_fr.properties时,将选择默认属性文件.我的系统区域设置是'en'.所以似乎MessageSource忽略了我设置为false的fallbackToSystemLocale属性.

此行为与Spring版本4.1.4以及4.1.5相同.

MessageSource配置:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="fallbackToSystemLocale" value="false"></property>
    <property name="basenames">
        <list>
            <value>locale/messages</value>
        </list>
    </property>
    <property name="defaultEncoding" value="UTF-8"></property>
</bean>
Run Code Online (Sandbox Code Playgroud)

收到消息:

String message = messageSource.getMessage("user.email.notFound", new Object[] {email}, null);
Run Code Online (Sandbox Code Playgroud)

谢谢你的建议!

java spring spring-java-config

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

如何使用Spring的Java Config保护具有多个领域的Web应用程序?

我有2种类型的资源的Web应用程序。

  • 网页
  • 网页服务

我想使用一个身份验证提供程序(即CAS)保护网页,并使用另一个身份验证提供程序(即BASIC身份验证)保护Web服务。

我找到了一个可以在这里工作的解决方案,但是它使用XML,并且如果可能的话,我宁愿不使用XML配置。

是否有Java Config解决方案?

java spring spring-security spring-java-config

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

IntelliJ:如何在没有persistence.xml的情况下启用JPAQL验证?

我使用Java配置并persistence.xml使用以下packagesToScan属性在没有任何文件的情况下在Spring中配置了JPA/Hibernate EntityManagerFactory:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {

    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(dataSource());
    emf.setPackagesToScan(new String[]{"ch.gbif.swissBiodivPortal.model.entities"});

    JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    emf.setJpaVendorAdapter(vendorAdapter);
    emf.setJpaProperties(hibernateConfigProperties());

    return emf;
}
Run Code Online (Sandbox Code Playgroud)

但是现在IntelliJ IDEA不再理解它,并且我的JPAQL查询中的实体用红色标记,并带有以下消息,并且自动完成不起作用:

无法解析符号'MyEntityName'

是否可以配置IntelliJ,以便它还扫描给定的包以获取JPA @Entity?

java spring jpa intellij-idea spring-java-config

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

Spring Integration的动态配置

从RabbitMQ连接规范列表到发布订阅频道上的一系列消费者的正确方法是什么?

也就是说,我说兔子配置如下:

rabbits:
    - hostname: blahblah
      vhost: blahlbah
      username: blahlbah
      password: blahlbalh
      exchange: blahblah
    - hostname: blahblah1
      vhost: blahlbah1
      username: blahlbah1
      password: blahlbalh1
      exchange: blahblah1
    ...
Run Code Online (Sandbox Code Playgroud)

我将这些编组成一个@NestedConfigurationProperty List<RabbitConfiguration>.我可以写一个@Bean方法,让我AmqpTemplate从其中一个List<RabbitConfiguration>,如下:

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public AmqpTemplate amqpTemplate(RabbitConfiguration rabbitConfiguration) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后,我可以基本上将其映射到IntegrationFlow:

@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public IntegrationFlow integrationFlow(AmqpTemplate amqpTemplate) {
    return IntegrationFlows.from(inboundPubSubChannel()).handle(Amqp.outboundAdapter(amqpTemplate)).get();
}
Run Code Online (Sandbox Code Playgroud)

然而,正是这样做的正确方法是什么,并由Spring处理List<RabbitConfiguration>结果List<IntegrationFlow>?当然不仅仅是:

@Bean
public List<IntegrationFlow> integrationFlows(List<RabbitConfiguration> rabbitConfigurations) {
    return rabbitConfigurations.stream()
        .map(this::amqpTemplate)
        .map(this::integrationFlow)
        .collect(toList())
}
Run Code Online (Sandbox Code Playgroud)

java spring spring-integration rabbitmq spring-java-config

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

使用Java配置设置Spring MVC 4的欢迎页面

我试图在Spring MVC 4中从XML迁移到完全基于 java 类的配置.到目前为止我所做的是创建一个简单的WebAppInitializer类和一个WebConfig类.

但是,我找不到配置我的欢迎页面的方法,这是我以前的摘录Web.xml:

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

java spring-mvc spring-java-config

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