小编M. *_*num的帖子

Spring Boot 1.3.3 @EnableResourceServer和@ EnableOAuth2Sso同时进行

我希望我的服务器是ResourceServer,它可以接受承载访问令牌

但是,如果此类令牌不存在,我想使用OAuth2Server对我的用户进行身份验证.

我尝试这样做:

@Configuration
@EnableOAuth2Sso
@EnableResourceServer
public class SecurityConfiguration extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated();
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,在这种情况下,只有@EnableResourceServer注释有效.它回来了

Full authentication is required to access this resource
Run Code Online (Sandbox Code Playgroud)

并且不要将我重定向到登录页面

我提到@Order重要的是,如果我添加@Order(0)注释,我将重定向到登录页面,但是,我无法使用Http标头中的access_token访问我的资源:

Authorization : Bearer 142042b2-342f-4f19-8f53-bea0bae061fc
Run Code Online (Sandbox Code Playgroud)

我怎样才能实现目标?我希望它同时使用Access令牌和SSO.

谢谢〜

spring spring-boot spring-security-oauth2

15
推荐指数
1
解决办法
2980
查看次数

spring:如何在使用@Autowired和context:component-scan自动连接原型bean时使用非默认构造函数?

假设您有一个原型bean类,如下所示:

@Component
@Scope("prototype")
public class Foobar {
    private String foo;
    public Foobar( String foo ) {
        this.foo = foo;
    }
}
Run Code Online (Sandbox Code Playgroud)

那么,是否可以使用@Autowired在另一个类中连接这样的bean,它应该使用非默认构造函数Foobar(String foo)来实例化bean?

update
在上面的示例中,构造函数参数String foo不可从应用程序上下文中获得,而是动态的.因此,对构造函数进行注释,@Autowired然后foo在上下文中指定某处,这似乎不是一个理想的解决方案.

java spring

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

Spring @Transactional和Hibernate @LockMode注释是如何相关的

我想知道交易和锁之间的关系.

更具体地说,Spring如何@Transactional与Hibernate的LockMode相关.https://docs.jboss.org/hibernate/orm/4.0/devguide/en-US/html/ch05.html. http://docs.spring.io/autorepo/docs/spring/4.2.x/spring-framework-reference/html/transaction.html

如果我不同时创建Session对象指定任何锁,并使用@TransactionalreadOnly作为false,我使用悲观Concurrenct控制.

如果有人能告诉我(乐观/悲观)并发控制和事务之间的关系,那将是一个很大的帮助.

谢谢Vivek

java spring hibernate locking transactions

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

SpringRunner 是否应该在 Spring Boot 中与 Junit 5 一起使用

我对 SpringRunner 类感到困惑。\n来自 Stackoverflow 和 google 确实理解了这一点SpringRunner并且SpringJunit4ClassRunner它们是一回事。

\n\n
@RunWith(SpringRunner.class)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我对这门课的理解是:

\n\n
    \n
  • 顾名思义,它是一个“运行程序”(它将运行 Junit 测试)
  • \n
  • 在使用注释执行单元测试时使用此类@RunWith
  • \n
  • 此注释有助于加载 spring 应用程序上下文并帮助 \xe2\x80\x98autowire\xe2\x80\x99 依赖项。
  • \n
  • 仅当我们需要 spring 上下文 bean 依赖项时才应使用此类。
  • \n
  • 所以这个类应该谨慎使用,否则我们将在测试过程中不必要地加载 spring 上下文。
  • \n
  • 此类帮助 Junit 和 Spring 协作。
  • \n
\n\n

有了 Junit5 和 spring boot,这个类不再需要了吗?

\n\n

如果是的话,我们应该在 Spring Boot 和 Junit5 环境中使用什么?

\n

java spring spring-boot

14
推荐指数
3
解决办法
1万
查看次数

如何在弹簧靴中注册自定义转换器?

我使用spring-boot-starter-jdbc(v1.3.0)编写应用程序.

我遇到的问题:BeanPropertyRowMapper失败的实例,因为它无法转换java.sql.Timestampjava.time.LocalDateTime.

为了复制这个问题,我实现 org.springframework.core.convert.converter.Converter了这些类型.

public class TimeStampToLocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> {

    @Override
    public LocalDateTime convert(Timestamp s) {
        return s.toLocalDateTime();
    }
}
Run Code Online (Sandbox Code Playgroud)

我的问题是:如何让我提供TimeStampToLocalDateTimeConverterBeanPropertyRowMapper.

更一般的问题是,如何注册我的转换器,以使它们在系统范围内可用?

以下代码将我们带到NullPointerException初始化阶段:

private Set<Converter> getConverters() {
    Set<Converter> converters = new HashSet<Converter>();
    converters.add(new TimeStampToLocalDateTimeConverter());
    converters.add(new LocalDateTimeToTimestampConverter());

    return converters;
}

@Bean(name="conversionService")
public ConversionService getConversionService() {
    ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
    bean.setConverters(getConverters()); 
    bean.afterPropertiesSet();
    return bean.getObject();
}    
Run Code Online (Sandbox Code Playgroud)

谢谢.

java spring spring-data spring-boot

13
推荐指数
1
解决办法
8850
查看次数

spring-boot从1.3.2升级到1.3.3:logback问题

从spring-boot 1.3.2升级到最近发布的1.3.3时,我们遇到了一个问题.

我们的应用程序一直在使用以下依赖项,每个依赖项都是最新的,没有问题:

    <neo4j.version>2.3.2</neo4j.version>
    <sdn.version>4.0.0.RELEASE</sdn.version>
    <sdn.rest.version>3.4.0.RELEASE</sdn.rest.version>
    <neo4j.ogm.version>1.1.5</neo4j.ogm.version>
Run Code Online (Sandbox Code Playgroud)

今天我升级了我们的spring-boot和基于Spring Data Neo4j的应用程序,该应用程序通过更改pom.xml从以下内容启动并与spring-boot 1.3.2.RELEASE一起运行良好:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
</parent>
Run Code Online (Sandbox Code Playgroud)

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.3.RELEASE</version>
</parent>
Run Code Online (Sandbox Code Playgroud)

由于这实际上是唯一的变化,应用程序现在无法启动以下错误:

...

Failed to instantiate [ch.qos.logback.classic.LoggerContext]
Reported exception:
java.lang.AbstractMethodError: ch.qos.logback.classic.pattern.EnsureExceptionHandling.process(Lch/qos/logback/core/pattern/Converter;)V
    at ch.qos.logback.core.pattern.PatternLayoutBase.start(PatternLayoutBase.java:88)
    at ch.qos.logback.classic.encoder.PatternLayoutEncoder.start(PatternLayoutEncoder.java:28)
    at ch.qos.logback.core.joran.action.NestedComplexPropertyIA.end(NestedComplexPropertyIA.java:167)
    at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:317)
    at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:196)
    at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:182)
    at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:62)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:149)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:135)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:99)
    at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:49)
    at ch.qos.logback.classic.util.ContextInitializer.configureByResource(ContextInitializer.java:77)
    at ch.qos.logback.classic.util.ContextInitializer.autoConfig(ContextInitializer.java:152)
    at org.slf4j.impl.StaticLoggerBinder.init(StaticLoggerBinder.java:85)
    at org.slf4j.impl.StaticLoggerBinder.<clinit>(StaticLoggerBinder.java:55)
    at org.slf4j.LoggerFactory.bind(LoggerFactory.java:143)
    at org.slf4j.LoggerFactory.performInitialization(LoggerFactory.java:122)
    at org.slf4j.LoggerFactory.getILoggerFactory(LoggerFactory.java:378)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:328)
    at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:349)
    at com.mycompany.Application.<clinit>(Application.java:35)
Run Code Online (Sandbox Code Playgroud)

正如所料,返回1.3.2.RELEASE不会导致任何问题.

到目前为止搜索显示没有可追踪的踪迹.比较mvn dependency:tree使用spring-boot 1.3.2.RELEASE和1.3.3.RELEASE之间的输出,我可以看到ch.qos.logback:logback-classic和ch.qos.logback:logback-access …

spring logback neo4j spring-boot spring-data-neo4j-4

13
推荐指数
1
解决办法
8985
查看次数

我可以将@Requestparam注释用于Post请求吗?

我有这个控制器方法:

@PostMapping(
        value = "/createleave",
        params = {"start","end","hours","username"})
public void createLeave(@RequestParam(value = "start") String start,
                        @RequestParam(value = "end") String end,
                        @RequestParam(value = "hours") String hours,
                        @RequestParam(value = "username") String username){
    System.out.println("Entering createLeave " + start + " " + end + " " + hours + " " + username);
    LeaveQuery newLeaveQuery = new LeaveQuery();
    Account account = accountRepository.findByUsername(username);
    newLeaveQuery.setAccount(account);
    newLeaveQuery.setStartDate(new Date(Long.parseLong(start)));
    newLeaveQuery.setEndDate(new Date(Long.parseLong(end)));
    newLeaveQuery.setTotalHours(Integer.parseInt(hours));
    leaveQueryRepository.save(newLeaveQuery);
}
Run Code Online (Sandbox Code Playgroud)

但是,当我向此端点发送帖子请求时,我得到以下信息

"{"timestamp":1511444885321,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.UnsatisfiedServletRequestParameterException","message":"Parameter conditions \"start, end, hours, username\" not met for actual request …
Run Code Online (Sandbox Code Playgroud)

java spring spring-mvc spring-web

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

@PostAuthorize和@PostFilter注释的有效用例

我们刚刚迁移到春季安全3.0.82.0.8(可"升级到latestversion这是3.2.X作为我们的核心Spring库仍然在3.0.x的,我们计划以后升级时,经营许可证).

据我所知,我们现在有注释为确保类似的方法@PreAuthorize,@PostAuthorize,@Secured,@PreFilter@PostFilter.

我理解使用@PreAuthorize,这真的很有意义.但是不能想到你曾经使用@PostAuthorize@PostFilter注释的任何有效用例?

使用它的人可以向我解释使用它们的合理用例吗?

提前致谢!

java spring spring-security spring-security-acl

12
推荐指数
2
解决办法
3203
查看次数

通过Java配置通过CommonAnnotationBeanPostProcessor激活@PostConstruct的正确方法是什么?

我正在使用带有SpringBoot的Spring 4和带有Java配置的Spring-Web.

要让@PostConstructSpring在启动时执行带注释的方法,必须注册CommonAnnotationBeanPostProcessor上下文,否则将@PostConstruct被忽略.

在基于XML的Spring配置中,文档说要使用(在beans元素下)

<context:annotation-config/>
Run Code Online (Sandbox Code Playgroud)

我还看到了一个示例,其中注册是在单个bean的基础上完成的,如下所示:

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
Run Code Online (Sandbox Code Playgroud)

如果可能,我希望避免这种情况.我的项目不包含任何XML文件,并且在我的构建文件夹中没有为我生成任何文件.

目前,我的解决方案是用我的类来注释@ComponentScan,我认为这会导致Spring检测并注册@Components和@Beans.不知何故,这导致CommonAnnotationBeanPostProcessor被调用,虽然我不知道为什么,但它解决了我的问题!

(这个类有一个@Autowired属性,在启动时为null - 因此需要进行初始化@PostConstruct)

但同样,我的问题是,使用Java配置实现这一目标的正确方法是什么?谢谢!

spring spring-annotations

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

没有DataSource的Spring启动+弹簧批处理

我正在尝试在spring boot项目中配置spring批处理,我想在没有数据源的情况下使用它.我发现这ResourcelessTransactionManager是要走的路,但我不能让它发挥作用.问题是我已经定义了另外3个dataSources,但我不想在springBatch中使用它们中的任何一个.

我已经检查了默认实现DefaultBatchConfigurer,如果它无法找到dataSource,它将完全按照我的意愿执行.问题是我有3个并且不想使用任何.

请不要建议在内存数据库中使用hsql或其他,因为我不想要它.

java spring spring-batch spring-boot

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