Original source: Csdn Deng
This article describes some of the simple and easy-to-use techniques for Java API Security and performance, including some of the recommendations in the framework for securing API Key and developing Web service.
Programmers like to use api! For example, build APIs for app apps or as part of a microservices architecture architecture. Of course, the premise of using the API is to make your job easier. Efforts to streamline development and improve productivity sometimes mean finding new class libraries or processes (or reducing processes). For many development teams, managing authentication and access control for their apps and APIs can take a lot of time, so we need to share some tips that will save you time, reduce the amount of code written, and make your app more secure and maintainable.
Let's introduce the background of this article: Okta is a Java application built on the rest, JSON API, built using the spring framework. Our company's application is to save the user's credentials and other company's sensitive data, so for us, security is the most important. So my first requirement for these techniques is that they can help make your Java application more secure.
These recommendations should be generic for any type of Java application. They will help you write code faster, but with less code and more security: it's really a win for three! 1. Do not implement the security framework on your own
Seriously, don't try to implement security code yourself, it's too hard.
Almost everyone knows to avoid implementing algorithms such as encryption. The same goes for the rest of your app's security stack, which can take a lot of money and get a lot of risk. You are likely to make some mistakes. Since 1999, 89,373 CVE (Public Vulnerabilities and Exposures) have been released. Most of the discoveries that are made public are those who are very intelligent.
You might think that dealing with a simple use case (such as validating a user's password) is a simple matter--all you do is compare a pair of strings. That's the wrong thing to think. You need to verify the hash of the password, audit the number of attempts to log in, and reduce the attack on the dictionary, which is just the tip of the iceberg. Your best bet is to use existing, well-established libraries or frameworks, such as Apache's Shiro or spring security, to allow these frameworks to handle complex security issues. 2. Use TLS, always! Always use tls!
It's been 2017 years now, all websites should use HTTPS, even the company's intranet. Let's encrypt makes HTTPS easy and easy, which means you can no longer use insecure self-signed keys! You can even set up a tomcat or Nginx instance with certificate authentication locally.
It takes a simple line of code for your app to require TLS (HTTPS/SSL), and everyone should do it! If you use the Apache Shiro Framework, you only need to set properties:
[URLs] /** = SSL
If you are using spring Security, simply call a method when you set up httpsecurity.
Http.requireschannel (). Anyrequest (). Requiressecure ();
In spring boot, you only need to set some properties, as follows:
server.port=8443server.ssl.key-store=classpath:keystore.jksserver.ssl.key-store-password= Secretserver.ssl.key-password=another-secret
3. Create a Web service using spring boot
Spring boot is a simplification of the spring platform, making it easy to write spring applications, such as the idea mentioned in the article "12 factors considered in app applications" with very little code. If you're still coding using the build War package, Spring boot is worth learning. Using spring boot can be complex, different types of applications, such as the use of simple annotations (@EnableResourceServer) to build an OAuth resource server, or to change its port with simple properties:
Server.port = 8090
If you do not like to use Springboot, then you can use Dropwizard to build the Jax-rs technology stack. 4. Monitoring applications and performance metrics
If there is no data in the case it is difficult to find the error of the program. Spring boot uses actuator to make it easy to collect metrics data by adding a dependency to the application as follows:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>`
You can then enter/health or/metrics to check your app's health or metrics through your browser, after accessing the app's address. The Dropwizard framework implements the same functionality through/healthcheck and/metrics.
The following is the result of the spring boot app output through/metrics:
{ "Classes": 7704,"classes.loaded": 7704,"classes.unloaded": 0,"Counter.status.200.metrics": 1,"Gauge.response.metrics": 99.0,"Gc.ps_marksweep.count": 2,"Gc.ps_marksweep.time": 272,"Gc.ps_scavenge.count": 8,"Gc.ps_scavenge.time": 136,"Heap": 3728384,"heap.committed": 470016,"Heap.init": 262144,"heap.used": 207793,"httpsessions.active": 0,"Httpsessions.max":-1,"Instance.uptime": 25020,"Mem": 529086,"Mem.free": 262222,"Nonheap": 0,"nonheap.committed": 60608,"Nonheap.init": 2496,"nonheap.used": 59067,"Processors": 8,"Systemload.average": 5.56103515625,"Threads": 24,"Threads.daemon": 22,"Threads.peak": 28,"threads.totalstarted": 32,"Uptime": 37182}View Code5. Protect sensitive information
It is true that the API key is considered unsafe. The key is either sent by email or controlled by the source code control system. Maybe that's why they look more insecure than passwords, but they're just as sensitive. If you need to store the API key in a file, be sure to grant limited access to the file. For example, we recommend that the private directory
Okta and gives the file owner read-only permissions.
$ chmod u=r,go-rwx ~/.okta/okta.yaml
If you're creating an API for users who use your app, remember to remind them that if they don't have permissions set. SSH files are placed in your ~/.ssh directory, if not set permissions. GitHub puts them in the "danger zone" to remind users that this is very useful.
5 tips for using the Java API