- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试通过 HTTPS 在我编写的应用程序上设置基于 Web 的安全性,该应用程序使用 Spring Boot 来提供 RESTful 功能。该应用程序仅使用 HTTP 即可按预期工作。
我已经对我需要做什么来在应用程序中“启用”HTTPS 进行了大量研究(至少我是这么认为的),并将提供代码片段和配置来说明我所拥有的。
我认为我很接近,但它还没有奏效,我尝试过的各种方法都没有成功。
当前的实现不要求服务(服务器)验证客户端的凭据。此外,我不需要任何形式的“用户”身份验证。
这里是对当前设置的简要描述:
还有一个“虚拟”服务,它只是将当前时间返回给来自客户端的 GET 请求。简单的测试器。
所有这五个元素都作为 @Service
实现,任务规划器、“路线”服务和虚拟对象都有相应的 Controller (@RestController
),其中 REST 端点被映射。
我为三个服务(任务规划器和两个“路线”服务 - 虚拟机只使用其中一个“路线”证书)生成了证书,这些文件位于“ keystore ”位置。我还有一个“信任库”位置,其中包含生成 CA 的公钥。所有五个服务都有信任库。
我无法让客户端与任何服务对话(为简单起见,使用“虚拟”)。我还尝试通过 Web 浏览器访问虚拟端点,结果似乎表明通信管道的某些部分正在发生,但失败了。
下面是代码片段,希望能以代码方式显示图片。
服务器(以“虚拟”为例)
虚拟.java:
@Service
@Profile("dummy")
public class Dummy {
public String doIt() {
return Long.toString(System.currentTimeMillis());
}
}
虚拟 Controller .java:
@RestController
@RequestMapping("/rst")
@Profile("dummy")
public class DummyController {
@Autowired
private Dummy service;
@GetMapping(value = "/dummy", produces = "text/plain")
public String dummy() {
return service.doIt();
}
}
注意:下面的类和 application.yml 中的属性是我根据网络上的示例改编的 (https://github.com/indrabasak/spring-tls-example)。我不太理解已经定义的“角色”的概念。这里有很多我还是不明白。
安全配置.java:
@Configuration
@EnableWebSecurity
@EnableConfigurationProperties(SecurityAuthProperties.class)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private final Logger logger = LogManager.getLogger();
private final SecurityAuthProperties properties;
@Autowired
public SecurityConfiguration(SecurityAuthProperties properties) {
this.properties = properties;
}
@Override
public void configure(AuthenticationManagerBuilder auth) {
// properties.getUsers().forEach((key, value) -> {
// try {
// auth.inMemoryAuthentication()
// .passwordEncoder(PasswordEncoderFactories.createDelegatingPasswordEncoder())
// .withUser(value.getId()).password(value.getPassword()).roles(value.getRoles());
// logger.info("Added user " + value.getId() + " with password " + value.getPassword());
// } catch (Exception e) {
// throw new SecurityConfigurationException(
// "Problem encountered while setting up authentication mananger", e);
// }
// });
}
@Override
public void configure(HttpSecurity http) throws Exception {
properties.getEndpoints().forEach((key, value) -> {
try {
for (HttpMethod method : value.getMethods()) {
// http.authorizeRequests().antMatchers(method, value.getPath())
// .hasAnyAuthority(value.getRoles()).and().httpBasic().and().csrf().disable();
http.authorizeRequests().antMatchers(method, value.getPath()).permitAll().and()
.httpBasic().and().csrf().disable();
logger.info("Added security for path " + value.getPath() + " and method " + method);
}
} catch (Exception e) {
throw new SecurityConfigurationException(
"Problem encountered while setting up endpoint restrictions", e);
}
});
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Override
public void configure(WebSecurity web) {
// TODO - what (if anything) do we do here?
}
}
SecurityAuthProperties.java:(“用户”部分目前已禁用,因为我们未使用它。)
@ConfigurationProperties("security.auth")
public class SecurityAuthProperties {
private static final String ROLE_PREFIX = "ROLE_";
public static final String ROLE_ANONYMOUS = "ROLE_ANONYMOUS";
private Map<String, Endpoint> endpoints = new HashMap<>();
// private Map<String, User> users = new HashMap<>();
@PostConstruct
public void init() {
endpoints.forEach((key, value) -> {
List<String> roles = new ArrayList<>();
for (String role : value.getRoles()) {
roles.add(ROLE_PREFIX + role);
}
value.setRoles(roles.toArray(new String[0]));
});
// users.forEach((key, value) -> {
// if (value.getId() == null) {
// value.setId(key);
// }
//
// if (value.getEncoding() != null) {
// value.setPassword("{" + value.getEncoding().trim() + "}" + value.getPassword());
// } else {
// value.setPassword("{noop}" + value.getPassword());
// }
// });
}
public Map<String, Endpoint> getEndpoints() {
return endpoints;
}
public void setEndpoints(Map<String, Endpoint> endpoints) {
this.endpoints = endpoints;
}
// public Map<String, User> getUsers() {
// return users;
// }
//
// public void setUsers(Map<String, User> users) {
// this.users = users;
// }
public static class Endpoint {
private String path;
private HttpMethod[] methods;
private String[] roles;
// trivial getters/setters removed for brevity
public String[] getRoles() {
if (roles == null || roles.length == 0) {
roles = new String[1];
roles[0] = ROLE_ANONYMOUS;
}
return roles;
}
}
public static class User {
private String id;
private String encoding;
private String password;
private String[] roles;
// trivial getters/setters removed for brevity
public String[] getRoles() {
if (roles == null || roles.length == 0) {
roles = new String[1];
roles[0] = ROLE_ANONYMOUS;
}
return roles;
}
}
}
应用程序.yml:
...
server:
port: 8443
ssl:
enabled: true
protocol: TLS
trust-store-type: JKS
trust-store: classpath:truststore/server.truststore
trust-store-password: <password>
key-store-type: JKS
security:
auth:
endpoints:
endpoint1:
path: /rst/dummy
methods: GET
roles:
客户
客户端服务.java:
@Service
public class ClientService {
private final Logger logger = LogManager.getLogger();
private static final String REST_DUMMY = "rst/dummy";
// @Autowired
// private RestTemplate template;
@Value("${web.protocol:http}")
private String protocol;
@Value("${mission-planner.host:localhost}")
private String missionPlannerHost;
@Value("${mission-planner.port:8443}")
private int missionPlannerPort;
@Scheduled(fixedRate = 10000)
public void planMission() {
logger.info("ClientService.planMission()");
RestTemplate template = new RestTemplate();
String url = new URLBuilder.Builder().usingProtocol(protocol).onHost(missionPlannerHost)
.atPort(missionPlannerPort).atEndPoint(REST_DUMMY).build();
String response = template.getForObject(url, String.class);
}
}
我的一个大问题是,如果服务器不需要验证客户端,需要在客户端完成什么(如果有)“安全”配置?我确实有一堆类/配置试图在客户端执行此操作,但目前已禁用。
使用如图所示的代码,当我尝试与虚拟服务通信时,客户端出现异常:
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://localhost:8443/rst/dummy": sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
更新
我决定尝试将 server.ssl.key-alias
(我在运行配置中通过 -D 设置)更改为大写(这是证书似乎具有的),现在得到一个新的有趣的异常(exception)。注意:我还为客户端和虚拟服务设置了 javax.net.debug=ssl
。
scheduling-1, WRITE: TLSv1.2 Handshake, length = 196
scheduling-1, READ: TLSv1.2 Alert, length = 2
scheduling-1, RECV TLSv1.2 ALERT: fatal, handshake_failure
scheduling-1, called closeSocket()
scheduling-1, handling exception: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
[2019-08-09 13:28:45.648] scheduling-1 ERROR: support.TaskUtils$LoggingErrorHandler:96 - Unexpected error occurred in scheduled task.
org.springframework.web.client.ResourceAccessException: I/O error on GET request for "https://localhost:8443/rst/dummy": Received fatal alert: handshake_failure; nested exception is javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
这是我在服务上得到的:
matching alias: route_assessor_1
matching alias: route_assessor_1
qtp1060563153-39, fatal error: 40: no cipher suites in common
javax.net.ssl.SSLHandshakeException: no cipher suites in common
%% Invalidated: [Session-1, SSL_NULL_WITH_NULL_NULL]
qtp1060563153-39, SEND TLSv1.2 ALERT: fatal, description = handshake_failure
qtp1060563153-39, WRITE: TLSv1.2 Alert, length = 2
qtp1060563153-39, fatal: engine already closed. Rethrowing javax.net.ssl.SSLHandshakeException: no cipher suites in common
qtp1060563153-39, called closeOutbound()
qtp1060563153-39, closeOutboundInternal()
最佳答案
这似乎是 Activity 部件太多而忘记重新打开某些东西的情况。
经过相当多的挣扎并回到原始源代码 (https://github.com/indrabasak/spring-tls-example) 并试验了一段时间,我看不出作者的工作代码和我的非工作代码之间有任何显着差异。
然后其中一个突然出现的情况发生了,我意识到我没有在我的客户端中使用安全配置的 REST 模板(由于我现在不记得的原因它被注释掉了)。我只使用了一个普通的未配置模板。
我取消注释代码,你看,客户端现在验证服务器的证书。
下一个问题。
关于java - RESTful 客户端/服务器的 HTTPS 通信问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57433701/
关闭。这个问题是off-topic .它目前不接受答案。 想要改进这个问题? Update the question所以它是on-topic用于堆栈溢出。 关闭 12 年前。 Improve thi
我有一个动态网格,其中的数据功能需要正常工作,这样我才能逐步复制网格中的数据。假设在第 5 行中,我输入 10,则从第 6 行开始的后续行应从 11 开始读取,依此类推。 如果我转到空白的第一行并输入
我有一个关于我的按钮消失的问题 我已经把一个图像作为我的按钮 用这个函数动画 function example_animate(px) { $('#cont
我有一个具有 Facebook 连接和经典用户名/密码登录的网站。目前,如果用户单击 facebook_connect 按钮,系统即可运行。但是,我想将现有帐户链接到 facebook,因为用户可以选
我有一个正在为 iOS 开发的应用程序,该应用程序执行以下操作 加载和设置注释并启动核心定位和缩放到位置。 map 上有很多注释,从数据加载不会花很长时间,但将它们实际渲染到 map 上需要一段时间。
我被推荐使用 Heroku for Ruby on Rails 托管,到目前为止,我认为我真的会喜欢它。只是想知道是否有人可以帮助我找出问题所在。 我按照那里的说明在该网站上创建应用程序,创建并提交
我看过很多关于 SSL 错误的帖子和信息,我自己也偶然发现了一个。 我正在尝试使用 GlobalSign CA BE 证书通过 Android WebView 访问网页,但出现了不可信错误。 对于大多
我想开始使用 OpenGL 3+ 和 4,但我在使用 Glew 时遇到了问题。我试图将 glew32.lib 包含在附加依赖项中,并且我已将库和 .dll 移动到主文件夹中,因此不应该有任何路径问题。
我已经盯着这两个下载页面的源代码看了一段时间,但我似乎找不到问题。 我有两个下载页面,一个 javascript 可以工作,一个没有。 工作:http://justupload.it/v/lfd7不是
我一直在使用 jQuery,只是尝试在单击链接时替换文本字段以及隐藏/显示内容项。它似乎在 IE 中工作得很好,但我似乎无法让它在 FF 中工作。 我的 jQuery: $(function() {
我正在尝试为 NDK 编译套接字库,但出现以下两个错误: error: 'close' was not declared in this scope 和 error: 'min' is not a m
我正在使用 Selenium 浏览器自动化框架测试网站。在测试过程中,我切换到特定的框架,我们将其称为“frame_1”。后来,我在 Select 类中使用了 deselectAll() 方法。不久之
我正在尝试通过 Python 创建到 Heroku PostgreSQL 数据库的连接。我将 Windows10 与 Python 3.6.8 和 PostgreSQL 9.6 一起使用。 我从“ht
我有一个包含 2 列的数据框,我想根据两列之间的比较创建第三列。 所以逻辑是:第 1 列 val = 3,第 2 列 val = 4,因此新列值什么都没有 第 1 列 val = 3,第 2 列 va
我想知道如何调试 iphone 5 中的 css 问题。 我尝试使用 firelite 插件。但是从纵向旋转到横向时,火石占据了整个屏幕。 有没有其他方法可以调试 iphone 5 中的 css 问题
所以我有点难以理解为什么这不起作用。我正在尝试替换我正在处理的示例站点上的类别复选框。我试图让它做以下事情:未选中时以一种方式出现,悬停时以另一种方式出现(选中或未选中)选中时以第三种方式出现(而不是
Javascript CSS 问题: 我正在使用一个文本框来写入一个 div。我使用以下 javascript 获取文本框来执行此操作: function process_input(){
你好,我很难理解 P、NP 和多项式时间缩减的主题。我试过在网上搜索它并问过我的一些 friend ,但我没有得到任何好的答案。 我想问一个关于这个话题的一般性问题: 设 A,B 为 P 中的语言(或
你好,我一直在研究 https://leetcode.com/problems/2-keys-keyboard/并想到了这个动态规划问题。 您从空白页上的“A”开始,完成后得到一个数字 n,页面上应该
我正在使用 Cocoapods 和 KIF 在 Xcode 服务器上运行持续集成。我已经成功地为一个项目设置了它来报告每次提交。我现在正在使用第二个项目并收到错误: Bot Issue: warnin
我是一名优秀的程序员,十分优秀!