<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: PRASAD NAIK</title>
    <description>The latest articles on Forem by PRASAD NAIK (@prasad_naik_c72a042c7615e).</description>
    <link>https://forem.com/prasad_naik_c72a042c7615e</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3641936%2Ff41d1957-4502-41e5-88ba-b6c681cd3e7f.png</url>
      <title>Forem: PRASAD NAIK</title>
      <link>https://forem.com/prasad_naik_c72a042c7615e</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/prasad_naik_c72a042c7615e"/>
    <language>en</language>
    <item>
      <title>🚀 Modern Security Guide for Java Developers</title>
      <dc:creator>PRASAD NAIK</dc:creator>
      <pubDate>Tue, 02 Dec 2025 17:48:50 +0000</pubDate>
      <link>https://forem.com/prasad_naik_c72a042c7615e/modern-security-guide-for-java-developers-4ho2</link>
      <guid>https://forem.com/prasad_naik_c72a042c7615e/modern-security-guide-for-java-developers-4ho2</guid>
      <description>&lt;p&gt;&lt;strong&gt;Subtitle:&lt;/strong&gt; OAuth 2.0, JWT, Asymmetric Encryption, Zero-Trust, Hardening Headers, API Gateway, &amp;amp; Load Balancers.&lt;/p&gt;

&lt;p&gt;Most developers think security ends at:&lt;/p&gt;

&lt;p&gt;Login → JWT → Authenticated ❌&lt;/p&gt;

&lt;p&gt;But enterprise-grade systems demand &lt;strong&gt;multi-layer security + zero-trust enforcement.&lt;/strong&gt; 🛡️&lt;/p&gt;

&lt;p&gt;Here is a practical, production-ready guide to hardening your Java architecture.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔐 1. OAuth 2.0 + Zero-Trust: Foundation of Modern Auth
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;OAuth 2.0 is authorization, not authentication.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your backend must treat every request as hostile.&lt;/p&gt;

&lt;p&gt;📌 Core Flow:&lt;/p&gt;

&lt;p&gt;Client → API Gateway → Authorization Server → Resource Server&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;🧠 Zero-Trust Rules:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;✔ &lt;strong&gt;Always verify WHO&lt;/strong&gt; the user is.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✔ &lt;strong&gt;Always verify WHAT&lt;/strong&gt; they can access.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✔ &lt;strong&gt;No implicit trust&lt;/strong&gt; — even inside your VPC.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;✔ &lt;strong&gt;Tokens&lt;/strong&gt; should always be short-lived.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🔑 2. JWT — Use Asymmetric Keys (RS256)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;HS256&lt;/strong&gt; = Risky shared secret ❌&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;RS256&lt;/strong&gt; = Private signing + Public verification ✔&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Key Strategy:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Private Key:&lt;/strong&gt; Stays &lt;em&gt;only&lt;/em&gt; in the Authorization Server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Public Key:&lt;/strong&gt; Shared to Gateway + Microservices for validation.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;🔐 Step 1: Generate RSA Keys&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bash&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openssl genrsa -out private.pem 4096
openssl rsa -in private.pem -pubout -out public.pem

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;🔎 Step 2: Spring Boot JWT Validation (Public Key)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
        .authorizeHttpRequests(auth -&amp;gt; auth
            .requestMatchers("/public/**").permitAll()
            .anyRequest().authenticated()
        )
        .oauth2ResourceServer(oauth -&amp;gt; oauth
            .jwt(jwt -&amp;gt; jwt.publicKey(publicKey()))
        )
        .sessionManagement(session -&amp;gt; session
            .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
        );
    return http.build();
}

@Bean
public RSAPublicKey publicKey() throws Exception {
    String key = Files.readString(Path.of("public.pem"))
            .replace("-----BEGIN PUBLIC KEY-----", "")
            .replace("-----END PUBLIC KEY-----", "")
            .replaceAll("\\s", "");

    byte[] decoded = Base64.getDecoder().decode(key);
    return (RSAPublicKey) KeyFactory
            .getInstance("RSA")
            .generatePublic(new X509EncodedKeySpec(decoded));
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🛡️ 3. Hardening with Security Headers
&lt;/h2&gt;

&lt;p&gt;Spring Security defaults are good, but enterprise apps need explicit hardening.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Defense List:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CSP (Content Security Policy):&lt;/strong&gt; Blocks malicious JS injections (Prevents 95% of XSS).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;HSTS:&lt;/strong&gt; Forces HTTPS and stops SSL downgrades.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;X-Frame-Options:&lt;/strong&gt; Disables framing to prevent Clickjacking.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;X-Content-Type-Options:&lt;/strong&gt; Blocks MIME sniffing (drive-by attacks).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;🔧 Secure Header Config:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http.headers(headers -&amp;gt; headers
    .contentSecurityPolicy(csp -&amp;gt; csp
        .policyDirectives("default-src 'self'; script-src 'self'")
    )
    .xssProtection(xss -&amp;gt; xss.block(true))
    .frameOptions(HeadersConfigurer.FrameOptionsConfig::deny)
    .httpStrictTransportSecurity(hsts -&amp;gt; hsts
        .includeSubDomains(true)
        .maxAgeInSeconds(31536000)
    )
    .contentTypeOptions(Customizer.withDefaults())
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧪 4. CSRF Protection — Correct Usage
&lt;/h2&gt;

&lt;p&gt;Most devs misconfigure this. The rule is simple:&lt;/p&gt;

&lt;p&gt;⚠ If using JWT in headers:&lt;/p&gt;

&lt;p&gt;Disable CSRF. The token prevents the attack.&lt;/p&gt;

&lt;p&gt;Java&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;http.csrf(csrf -&amp;gt; csrf.disable());

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;⚠ If using Cookie-based auth:&lt;/p&gt;

&lt;p&gt;You MUST enable CSRF.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧼 5. Prevent XSS — Sanitize User Input
&lt;/h2&gt;

&lt;p&gt;Never trust UI inputs. Never log raw data from users.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Validate&lt;/strong&gt; length.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Remove&lt;/strong&gt; scripts.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Java&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Google's JSON Sanitizer or similar library
String sanitized = JsonSanitizer.sanitize(userInput);

// Logging sanitized data only
log.info("User input: {}", sanitized);

if (input.length() &amp;gt; 200) throw new BadRequestException();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🌐 6. API Gateway — First Security Checkpoint
&lt;/h2&gt;

&lt;p&gt;Your Gateway is your bouncer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Flow:&lt;/strong&gt; &lt;code&gt;Client → WAF → Load Balancer → API Gateway → Microservices&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Gateway Responsibilities:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Centralized Auth&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JWT validation&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rate-limits &amp;amp; IP blocklists&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Route isolation&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example — Spring Cloud Gateway Token Relay:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;YAML&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring:
  cloud:
    gateway:
      routes:
        - id: secure-service
          uri: http://localhost:8082
          predicates:
            - Path=/secure/**
          filters:
            - RemoveRequestHeader=Cookie
            - TokenRelay

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  ⚙️ 7. Stateless Load Balancing
&lt;/h2&gt;

&lt;p&gt;Stickiness is &lt;strong&gt;not&lt;/strong&gt; needed when using JWT. This allows your microservices to remain lightweight and scalable.&lt;/p&gt;

&lt;p&gt;Plaintext&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;       Client
         |
   Load Balancer
         ↓
Microservice A ↔ Microservice B
   (Stateless Architecture)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🚫 8. Block Dangerous Actuator Endpoints
&lt;/h2&gt;

&lt;p&gt;If you expose Actuator without filtering, you are leaking full system metadata.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;application.properties:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Properties&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;management.endpoints.web.exposure.include=health,info
management.endpoints.web.exposure.exclude=env,beans

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🔐 9. Password Encryption
&lt;/h2&gt;

&lt;p&gt;Never roll your own crypto. Use BCrypt. It is slow by design, making it secure against brute-force attacks.&lt;/p&gt;

&lt;p&gt;Java&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Bean
public PasswordEncoder passwordEncoder() {
    return new BCryptPasswordEncoder(12);
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  🧩 10. Full Security Architecture
&lt;/h2&gt;

&lt;p&gt;A complete enterprise control list looks like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Layer 1 (Perimeter):&lt;/strong&gt; WAF, DDoS Mitigation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Layer 2 (Network):&lt;/strong&gt; Zero-Trust, TLS 1.3.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Layer 3 (Gateway):&lt;/strong&gt; Auth, Rate limits.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Layer 4 (Application):&lt;/strong&gt; OAuth2, JWT RS256.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Layer 5 (Headers):&lt;/strong&gt; CSP, HSTS, X-Frame-Options.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Layer 6 (Code):&lt;/strong&gt; Input Validation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Layer 7 (Secrets):&lt;/strong&gt; Vault / AWS Secrets Manager.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Layer 8 (Monitoring):&lt;/strong&gt; SIEM, Audit Logs.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;🗺️ The Architectural Blueprint&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;📱 Client
     ↓ (TLS 1.3)
🌐 API Gateway (JWT validation, throttling)
     ↓
🔐 Microservices (RBAC + Scopes)
     ↓
🗄 Encrypted Database (Least Privilege Access)

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🎯 Final Takeaway
&lt;/h2&gt;

&lt;p&gt;Most projects secure only the login page.&lt;/p&gt;

&lt;p&gt;Enterprise systems require security at EVERY layer.&lt;/p&gt;

&lt;p&gt;If you adopt even 50% of this guide, you’ll already be ahead of 90% of developers. 🚀&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>java</category>
      <category>security</category>
    </item>
  </channel>
</rss>
