小编lus*_*uso的帖子

spring @Autowire property vs setter

anotate @Autowired与某个属性或在setter中执行此操作有什么区别?

据我所知他们都有相同的结果,但有没有理由使用一个而不是另一个?

更新(更简洁)

这有什么区别

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   private SpellChecker spellChecker;

   @Autowired
   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }

   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}
Run Code Online (Sandbox Code Playgroud)

还有这个

package com.tutorialspoint;

import org.springframework.beans.factory.annotation.Autowired;

public class TextEditor {
   @Autowired
   private SpellChecker spellChecker;

   public TextEditor() {
      System.out.println("Inside TextEditor constructor." );
   }

   public void spellCheck(){
      spellChecker.checkSpelling();
   }
}
Run Code Online (Sandbox Code Playgroud)

java spring dependency-injection

31
推荐指数
4
解决办法
3万
查看次数

使用Node.js进行多个MySQL查询的方法

我是事件/回调样式编程和NodeJS的新手.我正在尝试实现一个使用node-mysql模块提供ddbb数据的小http服务器.

我的问题来自查询结构.由于经常有查询需要先前查询的结果才能运行,因此我无法同时(异步)运行所有这些查询,因此我不得不等待一些结果.

我的第一种方法是同时运行所有非依赖查询,然后循环,直到所有这些查询都设置了一个标记,说我已经完成了所以我可以继续使用依赖(同步),但我不知道这是否是正确的方法.

像这样的东西:

function x(){
    var result_for_asynch_query_1 = null
    var result_for_asynch_query_2 = null

    mainLoop(){
        // call non-dependant query 1
        // call non-dependant query 2

        // loop until vars are != null

        // continue with queries that require data from the first ones
    }
}

//for each browser request
httpServer{
     call_to_x();
}.listen();
Run Code Online (Sandbox Code Playgroud)

这样我可以在最终结果中节省一些时间,因为我不会以串行方式等待所有响应,而只是等待最长的响应.

有一个共同的方法来做到这一点?我没有遵循任何设计模式?

javascript mysql node.js

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

Selenium:检查WebElement是否具有焦点

我期待像WebElement.isfocus()这样的东西......真的很简单,但我找到的唯一方法是使用

:focus 
Run Code Online (Sandbox Code Playgroud)

伪类.

这真的是一个不寻常的任务,因为没有找到大量的信息?

我知道这个SO主题,但距离那时已经差不多两年了.在最近的版本中没有什么新东西?

你知道一些解决方法吗?

selenium selenium-webdriver

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

添加一个正文到404 Not Found Exception

在使用JHipster生成的REST API中,我想抛出一些404异常.它通常用

return new ResponseEntity<>(HttpStatus.NOT_FOUND);
Run Code Online (Sandbox Code Playgroud)

其实际上导致对xhr请求的404响应.问题是,在正面,JHipster解析响应

angular.fromJson(result)
Run Code Online (Sandbox Code Playgroud)

当404是实际响应时,这样的结果是空的,这使得解析失败.

如果我指向一个未映射的URI,让/api/user我说当我的控制器映射到/api/users(注意复数)时,我从API获得的404中有一个正文:

{
    "timestamp": "2016-04-25T18:33:19.947+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/api/user/myuser/contact"
}
Run Code Online (Sandbox Code Playgroud)

这是正确解析角度.

我怎样才能创建这样的身体?这是春天抛出的异常还是扔掉它的tomcat?

我试过这个:在Spring-MVC控制器中触发404?但我无法设置响应的参数.

java rest spring-mvc spring-boot jhipster

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

Ubuntu + SVN:无法打开请求的SVN文件系统

我知道很多次都会问过这个问题.我相信我理解答案,但我仍然没有运气.

我都尝试one repomultiple repos配置,具有对他们俩的同样的问题.

所以,使用我感兴趣的配置:

<Location /svn>
  DAV svn
  #SVNPath /media/ssd/svn/test
  SVNParentPath /media/ssd/svn
  SVNListParentPath On
  AuthType Basic
  AuthName "Subversion Repository"
  AuthUserFile /etc/apache2/dav_svn.passwd
  Require valid-user
</Location>
Run Code Online (Sandbox Code Playgroud)

我的存储库

luso@bender:/media/ssd/svn$ ls -lart
total 16
drwx------ 7 luso     luso 4096 2012-09-15 16:20 ..
drwxr-sr-x 6 www-data svn  4096 2012-09-24 22:13 test
drwxrwsr-x 4 www-data svn  4096 2012-09-24 22:31 .
drwxr-sr-x 6 www-data svn  4096 2012-09-24 22:31 test2
Run Code Online (Sandbox Code Playgroud)

在项目内

luso@bender:/media/ssd/svn/test$ ls -lart
total 32
-rw-r--r-- 1 www-data svn  229 2012-09-24 22:13 …
Run Code Online (Sandbox Code Playgroud)

svn apache ubuntu webdav

10
推荐指数
1
解决办法
4万
查看次数

SecurityFilterChain .anyRequest().permitAll() 不允许 POST 请求

使用 Spring Boot 2.7 (Spring Security 5.7.1) 并尝试将 API 配置为资源服务器和 OAuth2 客户端,我发现了一个我无法理解的行为:

@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
                .authorizeHttpRequests((authorize) -> authorize
                        .mvcMatchers("/swagger-ui/**", "/api-docs/**").permitAll()
                        .anyRequest().permitAll())

                // register OAuth2 resource server
                .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)

                // register OAuth2 client
                .oauth2Client(withDefaults());

        return http.build();
    }
}
Run Code Online (Sandbox Code Playgroud)

检查日志,所有这些过滤器都适用

o.s.s.web.DefaultSecurityFilterChain     : Will secure any request with 

org.springframework.security.web.session.DisableEncodeUrlFilter@320ca97c,
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@3c592c0c,
org.springframework.security.web.context.SecurityContextPersistenceFilter@2b33e616,
org.springframework.security.web.header.HeaderWriterFilter@2e9bff08,
org.springframework.security.web.csrf.CsrfFilter@7926d092,
org.springframework.security.web.authentication.logout.LogoutFilter@37227aa7,
org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter@6f18445b,
org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter@42af2977,
org.springframework.security.web.savedrequest.RequestCacheAwareFilter@79e3f444, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1252d480,
org.springframework.security.web.authentication.AnonymousAuthenticationFilter@3979c6e8,
org.springframework.security.oauth2.client.web.OAuth2AuthorizationCodeGrantFilter@19faa9dc,
org.springframework.security.web.session.SessionManagementFilter@7d3b4646,
org.springframework.security.web.access.ExceptionTranslationFilter@6cb2d5ea,
Run Code Online (Sandbox Code Playgroud)

到目前为止,此配置在我正在保护的其他 API 中按预期工作。然而,在这个特定的情况下,并且没有保护任何端点,我看到:

我可以访问任何GET端点,但任何POST端点都会返回403 FORBIDDEN. …

authentication spring spring-security oauth-2.0 spring-boot

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

设计模式:避免切换决定哪个服务调用

对于项目,我们有一个Controller/Service/DAO架构.我们实现了对不同提供者API的调用,因此我们在每个控制器类中都得到了一些这样的样板代码:

enum {
    PARTNER_A, PARTNER_B, PARTNER_C
}

public class MyController {
    @Resource PartnerASearchService partnerASearchService;
    @Resource PartnerBSearchService partnerBSearchService;
    @Resource PartnerCSearchService partnerCSearchService;

    public search(InputForm form) {
        switch(form.getPartnerName()) {
           case PARTNER_A: partnerASearchService.search();
                            break;
           case PARTNER_B: partnerBSearchService.search();
                            break;
           case PARTNER_C: partnerCSearchService.search();
                            break;
        }
    }

    public otherMethod(InputForm form) {
        switch(form.getProvider()) {
           case PARTNER_A: partnerAOtherService.otherMethod();
                           break;
           case PARTNER_B: partnerBOtherService.otherMethod();
                           break;
           case PARTNER_C: partnerCOtherService.otherMethod();
                           break;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我可以使用哪种设计模式来摆脱每个控制器中的这个开关?我希望代码类似于下面的代码:

public class MyController {
    @Resource ServiceStrategy serviceStrategy;

    public search(InputForm form){
        serviceStrategy.search(form.getPartnerName())
        // or
        serviceStrategy.invoke(SEARCH, form.getPartnerName())
    }

    public otherMethod(InputForm …
Run Code Online (Sandbox Code Playgroud)

java design-patterns strategy-pattern

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

正文中带有额外参数的 OAuth2 客户端(受众)

看来使用Auth0,在M2M流程中,我们需要audience在授权请求中传递参数,并且将为此颁发令牌audience

curl --request POST \
  --url https://domain.eu.auth0.com/oauth/token \
  --header 'content-type: application/json' \
  --data '{"client_id":"xxxxx","client_secret":"xxxxx","audience":"my-api-audience","grant_type":"client_credentials"}'
Run Code Online (Sandbox Code Playgroud)

否则,会抛出错误

403 Forbidden: "{"error":"access_denied","error_description":"No audience parameter was provided, and no default audience has been configured"}"
Run Code Online (Sandbox Code Playgroud)

Client Credentials我尝试使用新的 Spring Security 5 方法和使用 WebClient 的 webflux 来通过 Spring Boot实现流程。

https://github.com/spring-projects/spring-security-samples/tree/main/servlet/spring-boot/java/oauth2/webclient

Spring 没有提供向 Auth 请求添加自定义参数的方法,正如这篇文章所示

https://github.com/spring-projects/spring-security/issues/6569

我必须实现一个自定义转换器。

一切似乎在启动时都注入得很好,但在访问客户端端点时从未调用转换后的内容,localhost/api/explicit因此我一直遇到这个audience问题。

WebClientConfig.java

@Configuration
public class WebClientConfig {
    @Value("${resource-uri}")
    String resourceUri;

    @Value("${wallet-audience}")
    String audience;

       @Bean
        WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {

            var oauth2 = new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);

            return …
Run Code Online (Sandbox Code Playgroud)

java spring-security oauth-2.0 spring-boot auth0

6
推荐指数
1
解决办法
5990
查看次数

使用XDebug和PHPStorm调试Symfony2

我尝试尝试,但无法在PHPStorm中使用XDebug获得Symfony2的正确调试配置。

我已经检查过并重新检查过

并遵循了jetbrains教程,但是没有办法完成这项工作。

这是我正在尝试调试的带有Symfony2的rest api。到目前为止,我有一个带有Apache2的虚拟主机,可以在URL中正常工作

host: sd.lo
Run Code Online (Sandbox Code Playgroud)

api的路由前缀是

/api/v1
Run Code Online (Sandbox Code Playgroud)

所以访问

http://sd.lo/api/v1/clients
Run Code Online (Sandbox Code Playgroud)

工作正常,我收到客户清单

安装

XDebug已正确安装,如我的phpinfo()所示,并使用以下命令

在我的/usr/local/etc/php/5.5/conf.d/ext-xdebug.ini(已正确加载)中,我有:

[xdebug]
zend_extension="/usr/local/Cellar/php55-xdebug/2.3.3/xdebug.so"
xdebug.profiler_output_dir="/tmp/xdebug/"
xdebug.profiler_enable=on
xdebug.remote_enable=on
xdebug.remote_host="localhost"
xdebug.remote_port=9000
xdebug.remote_handler="dbgp"
xdebug.idekey = PHPSTORM
Run Code Online (Sandbox Code Playgroud)

并与命令

/usr/local/Cellar/php55/5.5.28/bin/php -v

PHP 5.5.28 (cli) (built: Aug 13 2015 14:02:41) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
    with Xdebug v2.3.3, Copyright (c) 2002-2015, by Derick Rethans
Run Code Online (Sandbox Code Playgroud)

在PHPStorm中

我已经在PHPStorm中正确设置了PHP解释器(我在OSX中安装了多个PHP版本。Brew安装是我要使用的安装方式),因此:

Path executable: /usr/local/Cellar/php55/5.5.28/bin/php
Debugger: XDebug 2.3.3 so is correct
Run Code Online (Sandbox Code Playgroud)

在“ PHPStorm首选项”>“ PHP”>“服务器”中,我有一个服务器

Name: Project.com …
Run Code Online (Sandbox Code Playgroud)

php debugging xdebug symfony phpstorm

5
推荐指数
0
解决办法
2252
查看次数

Liquibase部分提交变更集

在MySQL上使用liquibase v3.6.3。如果我理解正确,默认情况下,每个CHANGESET都会运行到SQL事务中。但是,在我看来,事务是在CHANGE基础上提交的。运行此脚本时

databaseChangeLog:
  - changeSet:
      id: changeset-
      changes:        
         - renameTable:
            oldTableName: old_table
            newTableName: new_table
        - addColumn:
            columns:
              - column:
                  name: test_column_name
                  type: varchar(255)
                  tableName: other_table
Run Code Online (Sandbox Code Playgroud)

如果addColumn标记由于某些SQL异常(例如约束检查或其他)而失败,则由于变更集失败,因此不会更改databasechangelog表,这是我不希望的。但是,firs语句DID通过了,我的表现在称为new_table

当然,如果我纠正了导致第二个失败并重试更新的问题,则它将失败,因为old_table不再存在。

我知道liquibase文档中的这一段

Liquibase尝试执行最后提交的事务中的每个changeSet,如果有错误,则回滚。一些数据库将自动提交语句,这会干扰此事务的设置并可能导致意外的数据库状态。因此,通常最好每个changeSet仅包含一个更改,除非您希望将一组非自动提交的更改应用为事务(例如插入数据)。

https://www.liquibase.org/documentation/changeset.html

但我不太了解。自动提交是指自动提交交易。如果所有变更集都包装在事务中,为什么只传递一些变更?liquibase是否应回滚整个交易?

有什么最佳实践吗?我们不能在liquibase中手动设置交易吗?

mysql database liquibase

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

在JPA中使用Embeddable Id保持OneToMany

授权服务基于http://www.svlada.com/jwt-token-authentication-with-spring-boot/(遗憾的是它没有提供注册示例)

我有以下实体和服务:

User.java

package com.test.entity;

import javax.persistence.*;
import java.io.Serializable;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Entity
@Table(name = "user")
public class User implements Serializable {
  private static final long serialVersionUID = 1322120000551624359L;

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column(name = "username")
  private String username;

  @Column(name = "password")
  private String password;

  @Column(name = "first_name")
  private String firstName;

  @Column(name = "last_name")
  private String lastName;

  @Column(name = "activated")
  private Boolean activated;

  @Column(name = "activation_token")
  private …
Run Code Online (Sandbox Code Playgroud)

java database spring hibernate jpa

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