小编Kel*_*lyM的帖子

带有JWT的Spring OAuth2 - 在分离Auth和Resource Server时无法将访问令牌转换为JSON

我希望使用Spring Boot创建一个可供多个资源服务器使用的OAuth2身份验证服务器.因此,我需要创建两个服务器作为独立的应用程序.我的主要参考文献是本文Stack Overflow问题.

引用的文章将两种服务器类型组合到一个应用程序中.我很难将它们分开.

我可以使用以下方法检索令牌:

curl testjwtclientid:XY7kmzoNzl100@localhost:8080/oauth/token -d grant_type=password -d username=john.doe -d password=jwtpass

此调用返回:

{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsidGVzdGp3dHJlc291cmNlaWQiXSwidXNlcl9uYW1lIjoiam9obi5kb2UiLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiXSwiZXhwIjoxNTE1MDUzOTMxLCJhdXRob3JpdGllcyI6WyJTVEFOREFSRF
9VU0VSIl0sImp0aSI6IjBhY2ZlOTA5LTI1Y2MtNGFmZS1iMjk5LTI3MmExNDRiNzFhZCIsImNsaWVudF9pZCI6InRlc3Rqd3RjbGllbnRpZCJ9.ctWt8uNR55HS2PH0OihcVnXuPuw_Z33_zk6wE1qx_5U","token_type":"bearer","expires_in":43199,"scope":"read w
rite","jti":"0acfe909-25cc-4afe-b299-272a144b71ad"}
Run Code Online (Sandbox Code Playgroud)

但是,每当我尝试使用令牌联系我的资源服务器时,都会收到错误:

    curl localhost:8090/springjwt/test -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsidGVzdGp3dHJlc291cmNlaWQiXSwidXNlcl9uYW1lIjoiam9obi5kb2UiLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiXSwiZXhwIjoxNTE1MDUzOTMxLCJhdXRob3JpdGllcyI6WyJTVEFOREFSRF9VU0VSIl0sImp0aSI6IjBhY2ZlOTA5LTI1Y2MtNGFmZS1iMjk5LTI3MmExNDRiNzFhZCIsImNsaWVudF9pZCI6InRlc3Rqd3RjbGllbnRpZCJ9.ctWt8uNR55HS2PH0OihcVnXuPuw_Z33_zk6wE1qx_5U"
Run Code Online (Sandbox Code Playgroud)

{"error":"invalid_token","error_description":"Cannot convert access token to JSON"}

Auth Server配置(来自文章):

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Value("${security.jwt.client-id}")
    private String clientId;

    @Value("${security.jwt.client-secret}")
    private String clientSecret;

    @Value("${security.jwt.grant-type}")
    private String grantType;

    @Value("${security.jwt.scope-read}")
    private String scopeRead;

    @Value("${security.jwt.scope-write}")
    private String scopeWrite = "write";

    @Value("${security.jwt.resource-ids}")
    private String resourceIds;

    @Autowired
    private TokenStore tokenStore;

    @Autowired
    private JwtAccessTokenConverter accessTokenConverter;

    @Autowired …
Run Code Online (Sandbox Code Playgroud)

java spring oauth spring-security jwt

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

Spring Boot JPA - 未设置“hibernate.dialect”时,对 DialectResolutionInfo 的访问不能为空

我有一个 Spring Boot JPA 应用程序。每当我尝试运行它时,它都会失败:

Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set        at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:100) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
Run Code Online (Sandbox Code Playgroud)

我在这里查看了相关问题,但大多数似乎与数据库尚未启动、凭据不正确等问题有关。但是,我能够与 PSQL 客户端连接得很好,所以这不是问题。我已经尝试过使用和不使用我的 application.properties 中指定的平台和驱动程序:

spring.datasource.url=jdbc:postgresql://xxxx/my_db
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.jpa.show-sql=true
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
Run Code Online (Sandbox Code Playgroud)

此外,在错误之上,应用程序实际上打印出以下内容:

2019-02-15 10:02:40.134  INFO 43204 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
Run Code Online (Sandbox Code Playgroud)

可能是什么问题?这个应用程序实际上正在运行;但是最近换了开发机器,现在连不上。我已经禁用了我机器上的防火墙,看看这是否可能是问题,但没有成功。同样,我能够通过 PSQL 进行连接,所以我真的不知道该怎么想。

堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManager' defined in class path resource [com/midamcorp/insuranceui/AppConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.persistence.EntityManager]: Factory method 'entityManager' …
Run Code Online (Sandbox Code Playgroud)

java database spring spring-data-jpa spring-boot

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

迁移到Spring Boot 2 - 安全编码密码看起来不像BCrypt

我有一个Spring Boot 1.5.9授权服务器,它使用BCrypt进行密码存储.我正在尝试迁移到2.0但是,我不再能够检索令牌以进行授权.

服务器的响应是:

    "timestamp": "2018-03-09T15:22:06.576+0000",
    "status": 401,
    "error": "Unauthorized",
    "message": "Unauthorized",
    "path": "/oauth/token"
}
Run Code Online (Sandbox Code Playgroud)

控制台输出以下内容:2018-03-09 09:22:06.553 WARN 20976 --- [nio-8090-exec-1] o.s.s.c.bcrypt.BCryptPasswordEncoder : Encoded password does not look like BCrypt.

这部分应用程序之前工作正常.我做的唯一更改是build.gradle文件(更改springBootVersion,添加io.spring.dependency-management插件和添加runtime('org.springframework.boot:spring-boot-devtools').

buildscript {
    ext {
        springBootVersion = '2.0.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

group = 'com.midamcorp'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
    mavenCentral() …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot

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

Spring Boot - 使用 JWT、OAuth 以及单独的资源和身份验证服务器

我正在尝试构建一个使用 JWT 令牌和 OAuth2 协议的 Spring 应用程序。由于本教程,我已经运行了身份验证服务器。但是,我正在努力使资源服务器正常运行。从这篇文章开始,并感谢对先前问题的回答,这是我目前的尝试:

资源服务器的安全配置:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Value("${security.signing-key}")
    private String signingKey;

    @Value("${security.encoding-strength}")
    private Integer clientID;

    @Value("${security.security-realm}")
    private String securityRealm;

    @Bean
    public JwtAccessTokenConverter accessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        converter.setVerifierKey(signingKey);
        return converter;
    }

    @Bean
    public TokenStore tokenStore() {
        return new JwtTokenStore(accessTokenConverter());
    }

    @Bean ResourceServerTokenServices tokenService() {
        DefaultTokenServices defaultTokenServices = new DefaultTokenServices();
        defaultTokenServices.setTokenStore(tokenStore());
        defaultTokenServices.setSupportRefreshToken(true);
        return defaultTokenServices;
    }
    @Override
    public AuthenticationManager authenticationManager() throws Exception {
        OAuth2AuthenticationManager …
Run Code Online (Sandbox Code Playgroud)

spring oauth spring-security jwt spring-security-oauth2

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

Spring Boot、PostgreSQL 和 Docker - 在容器中运行时连接被拒绝

我正在尝试构建一个由 Spring Boot 应用程序和 PostgreSQL 数据库组成的“服务”。当 Spring Boot 应用程序在我的本地机器上运行时,我已经能够从 Spring Boot 应用程序访问数据库(在容器中运行)。现在,当我尝试将 Spring Boot 应用程序移动到容器时,收到以下错误:

inventory_1  | 2018-01-20 18:43:06.108 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection] with root cause
inventory_1  |
inventory_1  | java.net.ConnectException: Connection refused (Connection refused)
Run Code Online (Sandbox Code Playgroud)

但是,我可以从本地机器连接到数据库: psql -h localhost -p 5000 -U kelly_psql -d …

postgresql spring docker spring-boot docker-compose

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

Spring Security 5 - 密码迁移

这是对早期问题的后续跟进.

我正在尝试将OAuth2应用程序迁移到Spring Boot 2/Security 5.根据我之前的问题(和)中的一条评论,似乎密码的存储格式正在发生变化.

在我原来的(Spring 1.5.9)应用程序中,我明确指定了BCrypt

// AppConfig

   @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
Run Code Online (Sandbox Code Playgroud)

// SecurityConfig

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {


auth.userDetailsService(userService)    
.passwordEncoder(passwordEncoder);
}
Run Code Online (Sandbox Code Playgroud)

这导致错误,密码"看起来不像BCrypt(我之前的问题的原因).

感谢回复我之前的问题的评论,似乎我需要使用{bcrypt}为存储的密码添加前缀.我还用以下代码替换了我的PassowrdEncoder @Bean:

PasswordEncoder passwordEncoder =
    PasswordEncoderFactories.createDelegatingPasswordEncoder();
Run Code Online (Sandbox Code Playgroud)

但是,这导致以下错误:

java.lang.IllegalArgumentException: There is no PasswordEncoder mapped for the id "null"

接下来,我尝试将更@Bean改为以下内容:

@Bean
public PasswordEncoder passwordEncoder() {

    String idForEncode = "bcrypt";
    Map encoders = new HashMap<>();
    encoders.put(idForEncode, new BCryptPasswordEncoder());

    encoders.put("pbkdf2", new Pbkdf2PasswordEncoder());
    encoders.put("scrypt", new SCryptPasswordEncoder()); …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot

7
推荐指数
0
解决办法
1417
查看次数

Android/Google Play - 应用程序在运行时可以更新

我正在构建一个旨在持续运行的应用程序.此外,用户在使用时会锁定应用程序(通过固定功能).当应用程序暂时未检测到用户交互时,它会自动取消,调用Android梦想服务,并显示各种屏幕保护程序.当用户点击设备时,它会"唤醒",进入主屏幕并自行复制.

我需要我的应用程序自动更新.但是,鉴于这种情况,我很难这样做.它似乎没有更新,或根据我的一位同事,更新但关闭了应用程序.

有没有办法让应用程序在运行时检测,下载和安装更新,然后,如有必要,重新启动自己?什么是最好的方法?

非常感谢.

android google-play

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

Java ModelMapper - 无法将 java.util.List 转换为 java.util.List

我正在使用 Java ModelMapper 库将 DTO 映射到 ORM@Entity对象。我有以下测试设置:

    public class MapperTest {

    @Autowired
    private ModelMapper mapper;

    @Autowired
    private TicketRepository ticketRepo;

    @Test
    public void testTicket() {
        Ticket ticket = ticketRepo.findById(4).get();

        TicketDTO dto = mapper.map(ticket, TicketDTO.class);
        assertThat(dto.getEquipmenDescription()).isEqualTo(ticket.getEquipment().getDescription());
        assertThat(dto.getEquipmentNotes()).isEqualTo(ticket.getEquipmentNotes());
        assertThat(dto.getId()).isEqualTo(ticket.getId());
        assertThat(dto.getNotes()).isEqualTo(ticket.getNotes());
        assertThat(dto.getOracleID()).isEqualTo(ticket.getOracleID());
        assertThat(dto.getPropertyID()).isEqualTo(ticket.getPropertyID());
        assertThat(dto.getNotes().size()).isEqualTo(ticket.getNotes().size());

        for (TicketNoteDTO note : dto.getNotes()) {
            assertThat(note.getId()).isNotEqualTo(0);
            assertThat(note.getIssueDate()).isNotNull();
            assertThat(note.getUserUserName()).isNotEmpty();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这失败并出现以下错误:

org.modelmapper.MappingException: ModelMapper mapping errors:

1) Converter org.modelmapper.internal.converter.CollectionConverter@501c6dba failed to convert java.util.List to java.util.List.
Run Code Online (Sandbox Code Playgroud)

以下是我的实体和相应的 DTO。为简洁起见,省略了 getter 和 setter。

   @Entity
    @Table(name = "ticket")
    public class …
Run Code Online (Sandbox Code Playgroud)

java modelmapper

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

Spring Data JPA - 数据库问题连接不可用,请求在 30000 毫秒后超时

我有一个非常典型的基于 Spring Boot 的 Web 应用程序,Spring Boot 版本 2.2.4,具有以下“启动器”:data-jpa、mail、security、web、thymeleaf。我使用 PostgreSQL 作为我的数据源和 Spring Session JDBC。该应用程序将正常运行几个小时,但最终会抛出与数据库连接相关的错误:

2020-02-07 02:36:42.891  WARN 14412 --- [https-jsse-nio-8445-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: null
2020-02-07 02:36:42.891 ERROR 14412 --- [https-jsse-nio-8445-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : HikariPool-1 - Connection is not available, request timed out after 30001ms.
2020-02-07 02:36:42.891 ERROR 14412 --- [https-jsse-nio-8445-exec-9] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] threw exception
Run Code Online (Sandbox Code Playgroud)

完整的堆栈跟踪:

    2020-02-07 02:36:42.891  WARN 14412 --- [https-jsse-nio-8445-exec-9] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: null
2020-02-07 02:36:42.891 ERROR 14412 --- [https-jsse-nio-8445-exec-9] …
Run Code Online (Sandbox Code Playgroud)

java hibernate spring-data-jpa spring-boot

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

HTML表单值PHP和空格

我正在为我的组织开展由其他开发人员启动的各种项目.这项工作的很大一部分涉及使用PHP进行表单处理.

我很惊讶地看到,对于许多表单值,前一个开发人员使用了空格(例如<input type="radio" value="DO NOT ENROLL" name="proceed" /> ).我想我知道他为什么这样做了 - 他发送它作为电子邮件,并且可以很容易地显示人类可读的值没有额外处理的格式,但是,作为习惯于"典型"编程命名约定(没有空格,驼峰大小写,pascal大小写等)的人,这确实让我有些不安.每当我处理HTML表单并分配值得,我相信我总是试图遵循"典型的"变量命名约定,但这可能有习惯而不是必要的练习.

现在,我并不是说他错了,但我试着对这个话题进行一些研究,并没有得出明确的结论.所以我的问题是 - 在分配给HTML表单输入元素的值中包含空格是否有任何问题 - 特别是当它将通过电子邮件发送时?如果是这样,可能会导致哪些复杂情况,并且是否有正确处理它们的方法,或者您是否建议保持实际表单值简单(例如,<input type="radio" value="decline" name="proceed" />然后处理它们以使其在提交表单后更具可读性).

感谢您的任何意见!

html php forms

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