{"id":53,"date":"2025-03-15T05:55:00","date_gmt":"2025-03-15T05:55:00","guid":{"rendered":"https:\/\/harshad-sonawane.com\/blog\/?p=53"},"modified":"2025-02-22T06:59:44","modified_gmt":"2025-02-22T06:59:44","slug":"spring-security-authentication-authorization-guide","status":"publish","type":"post","link":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/","title":{"rendered":"Spring Security: Implementing Authentication &amp; Authorization"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>Introduction<\/strong><\/h2>\n\n\n\n<p>Security is a critical component in modern web applications, and <strong><a href=\"https:\/\/harshad-sonawane.com\/blog\/two-factor-authentication-java-applications\/\">Spring Security<\/a><\/strong> is the de facto framework for handling <strong>authentication and authorization<\/strong> in <strong><a href=\"https:\/\/harshad-sonawane.com\/blog\/audit-logging-in-java-microservices-techniques-and-compliance-tips\/\">Spring Boot<\/a> applications<\/strong>. Whether you are developing a <strong>REST API, <a href=\"https:\/\/harshad-sonawane.com\/blog\/reduce-cloud-costs-java-applications\/\">microservices<\/a>, or monolithic applications<\/strong>, Spring Security provides robust security configurations and features to protect your application.<\/p>\n\n\n\n<p>This guide covers <strong>how to implement authentication and authorization<\/strong> using Spring Security, covering <strong>JWT, OAuth2, role-based access control (RBAC), and method-level security<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. Understanding Spring Security<\/strong><\/h2>\n\n\n\n<p>Spring Security is a powerful and customizable authentication and access control framework for <strong>Spring-based applications<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Key Features of Spring Security:<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Authentication &amp; Authorization<\/strong><\/li>\n\n\n\n<li><strong>CSRF Protection<\/strong><\/li>\n\n\n\n<li><strong>Session Management<\/strong><\/li>\n\n\n\n<li><strong>OAuth2 &amp; JWT Integration<\/strong><\/li>\n\n\n\n<li><strong>Method-Level Security<\/strong><\/li>\n\n\n\n<li><strong>Password Encoding &amp; Hashing<\/strong><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. Setting Up Spring Security in a Spring Boot Application<\/strong><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Add Spring Security Dependency<\/strong><\/h3>\n\n\n\n<p>Add the following dependency in <code>pom.xml<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"Java\" class=\"language-Java\">&lt;dependency>\n    &lt;groupId>org.springframework.boot&lt;\/groupId>\n    &lt;artifactId>spring-boot-starter-security&lt;\/artifactId>\n&lt;\/dependency><\/code><\/pre>\n\n\n\n<p>Spring Boot auto-configures a default security layer, requiring users to authenticate before accessing any endpoint.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Configure Security in Spring Boot<\/strong><\/h3>\n\n\n\n<p>Spring Security requires a <strong>SecurityFilterChain<\/strong> bean configuration in Spring Boot 3+.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"Java\" class=\"language-Java\">@Configuration\n@EnableWebSecurity\npublic class SecurityConfig {\n\n    @Bean\n    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {\n        http\n            .csrf(AbstractHttpConfigurer::disable)\n            .authorizeHttpRequests(auth -> auth\n                .requestMatchers(\"\/public\/**\").permitAll()\n                .anyRequest().authenticated())\n            .formLogin(Customizer.withDefaults());\n\n        return http.build();\n    }\n}<\/code><\/pre>\n\n\n\n<p>This configuration:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Disables CSRF (use with caution in REST APIs).<\/li>\n\n\n\n<li>Allows unauthenticated access to <code>\/public\/**<\/code>.<\/li>\n\n\n\n<li>Requires authentication for all other requests.<\/li>\n\n\n\n<li>Enables a default login form.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Implementing User Authentication<\/strong><\/h2>\n\n\n\n<p>Authentication is the process of verifying <strong>who a user is<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Create a User Entity &amp; Repository<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"Java\" class=\"language-Java\">@Entity\npublic class User {\n    @Id\n    @GeneratedValue(strategy = GenerationType.IDENTITY)\n    private Long id;\n    private String username;\n    private String password;\n    private String role;\n\n    \/\/ Getters and Setters\n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"Java\" class=\"language-Java\">@Repository\npublic interface UserRepository extends JpaRepository&lt;User, Long> {\n    Optional&lt;User> findByUsername(String username);\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Implement UserDetailsService<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"Java\" class=\"language-Java\">@Service\npublic class CustomUserDetailsService implements UserDetailsService {\n\n    @Autowired\n    private UserRepository userRepository;\n\n    @Override\n    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {\n        User user = userRepository.findByUsername(username)\n                .orElseThrow(() -> new UsernameNotFoundException(\"User not found\"));\n\n        return new User(user.getUsername(), user.getPassword(),\n                List.of(new SimpleGrantedAuthority(user.getRole())));\n    }\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Password Encoding<\/strong><\/h3>\n\n\n\n<p>Spring Security recommends using <strong>BCryptPasswordEncoder<\/strong> for storing passwords securely.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"Java\" class=\"language-Java\">@Bean\npublic PasswordEncoder passwordEncoder() {\n    return new BCryptPasswordEncoder();\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Implementing JWT Authentication<\/strong><\/h2>\n\n\n\n<p>JSON Web Tokens (JWT) are widely used for <strong>stateless authentication<\/strong> in Spring Security.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Add JWT Dependencies<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"Java\" class=\"language-Java\">&lt;dependency>\n    &lt;groupId>io.jsonwebtoken&lt;\/groupId>\n    &lt;artifactId>jjwt&lt;\/artifactId>\n    &lt;version>0.11.5&lt;\/version>\n&lt;\/dependency><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Generate and Validate JWT<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"Java\" class=\"language-Java\">@Component\npublic class JwtUtil {\n\n    private final String SECRET_KEY = \"mySecretKey\";\n\n    public String generateToken(String username) {\n        return Jwts.builder()\n                .setSubject(username)\n                .setIssuedAt(new Date())\n                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 60 * 60))\n                .signWith(SignatureAlgorithm.HS256, SECRET_KEY)\n                .compact();\n    }\n\n    public String extractUsername(String token) {\n        return Jwts.parser().setSigningKey(SECRET_KEY).parseClaimsJws(token).getBody().getSubject();\n    }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. Implementing Role-Based Authorization (RBAC)<\/strong><\/h2>\n\n\n\n<p>Authorization controls <strong>what users can do<\/strong> in the application.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"Java\" class=\"language-Java\">@Configuration\n@EnableMethodSecurity\npublic class MethodSecurityConfig {\n}<\/code><\/pre>\n\n\n\n<p>Use <strong>@PreAuthorize<\/strong> to restrict access to specific roles:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"Java\" class=\"language-Java\">@PreAuthorize(\"hasRole('ADMIN')\")\n@GetMapping(\"\/admin\")\npublic String adminEndpoint() {\n    return \"Admin Access Granted\";\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. Best Practices for Secure Spring Applications<\/strong><\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Use strong password encoding<\/strong> \u2013 Always hash passwords before storing them.<\/li>\n\n\n\n<li><strong>Enable HTTPS<\/strong> \u2013 Encrypt communication between clients and the server.<\/li>\n\n\n\n<li><strong>Implement token expiration &amp; refresh tokens<\/strong> \u2013 Prevent long-lived JWT tokens.<\/li>\n\n\n\n<li><strong>Use environment variables for secrets<\/strong> \u2013 Avoid hardcoding secrets.<\/li>\n\n\n\n<li><strong>Enable security logging<\/strong> \u2013 Monitor authentication attempts and failures.<\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Spring Security provides a <strong>comprehensive security framework<\/strong> for securing applications using <strong>authentication and authorization mechanisms<\/strong>. By implementing <strong>JWT, OAuth2, role-based access control, and method-level security<\/strong>, developers can ensure a secure environment for their applications.<\/p>\n\n\n\n<p>Would you like to see a <strong>step-by-step tutorial<\/strong> on implementing OAuth2 with Spring Security? Let us know in the comments!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"o-typing-delay-100ms ticss-27f7e3e9\"><o-anim-typing>&lt;> <strong>&#8220;Happy developing, one line at a time!&#8221;<\/strong> &lt;\/><\/o-anim-typing><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Security is a critical component in modern web applications, and Spring Security is the de facto framework for handling authentication and authorization in Spring Boot applications. Whether you are developing a REST API, microservices, or monolithic applications, Spring Security provides robust security configurations and features to protect your application. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":74,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_themeisle_gutenberg_block_has_review":false,"footnotes":"","jetpack_publicize_message":"Authentication & authorization are key to protecting APIs. Spring Security, JWT, and OAuth2 make it seamless. Learn how to implement them!\n\n#SpringBoot #SpringSecurity #Authentication #Authorization #Java\n\n\ud83d\udd17 Full blog here:","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[36],"tags":[16,77,74,83,17,87,75,89,73,79,82,81,88,80,76,86,78,85,84],"class_list":["post-53","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java-spring-boot-aws-cloud","tag-api-security","tag-authentication-in-spring-boot","tag-authorization-in-spring-boot","tag-java-security","tag-jwt-authentication","tag-microservices-security","tag-oauth2-spring-boot","tag-password-hashing","tag-pring-security","tag-rbac-spring-security","tag-role-based-access-control","tag-secure-spring-boot","tag-spring-boot-login","tag-spring-boot-oauth2","tag-spring-boot-security","tag-spring-security-best-practices","tag-spring-security-jwt","tag-token-based-authentication","tag-web-security"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Security: Implementing Authentication &amp; Authorization - &lt;&gt;HARSHAD&#039;s Dev Diary&lt;\/&gt;<\/title>\n<meta name=\"description\" content=\"Learn how to implement authentication and authorization in Spring Boot using Spring Security. Secure your application with JWT, OAuth2, ...\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Security: Implementing Authentication &amp; Authorization - &lt;&gt;HARSHAD&#039;s Dev Diary&lt;\/&gt;\" \/>\n<meta property=\"og:description\" content=\"Learn how to implement authentication and authorization in Spring Boot using Spring Security. Secure your application with JWT, OAuth2, ...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/\" \/>\n<meta property=\"og:site_name\" content=\"&lt;&gt;HARSHAD&#039;s Dev Diary&lt;\/&gt;\" \/>\n<meta property=\"article:published_time\" content=\"2025-03-15T05:55:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/harshad-sonawane.com\/blog\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-02-15-17.42.49-A-minimalistic-and-illustrative-digital-artwork-representing-authentication-and-authorization-in-Spring-Security.-The-image-should-depict-a-secure-log.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1024\" \/>\n\t<meta property=\"og:image:height\" content=\"1024\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"HS\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"HS\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/\"},\"author\":{\"name\":\"HS\",\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/#\\\/schema\\\/person\\\/d82781218ba30c34fa81b49e8393681e\"},\"headline\":\"Spring Security: Implementing Authentication &amp; Authorization\",\"datePublished\":\"2025-03-15T05:55:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/\"},\"wordCount\":380,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/#\\\/schema\\\/person\\\/d82781218ba30c34fa81b49e8393681e\"},\"image\":{\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/DALL\u00b7E-2025-02-15-17.42.49-A-minimalistic-and-illustrative-digital-artwork-representing-authentication-and-authorization-in-Spring-Security.-The-image-should-depict-a-secure-log.webp\",\"keywords\":[\"API Security\",\"Authentication in Spring Boot\",\"Authorization in Spring Boot\",\"Java Security\",\"JWT Authentication\",\"Microservices Security\",\"OAuth2 Spring Boot\",\"Password Hashing\",\"pring Security\",\"RBAC Spring Security\",\"Role-Based Access Control\",\"Secure Spring Boot\",\"Spring Boot Login\",\"Spring Boot OAuth2\",\"Spring Boot Security\",\"Spring Security Best Practices\",\"Spring Security JWT\",\"Token-Based Authentication\",\"Web Security\"],\"articleSection\":[\"Java, Spring Boot, AWS, Cloud\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/\",\"url\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/\",\"name\":\"Spring Security: Implementing Authentication &amp; Authorization - &lt;&gt;HARSHAD&#039;s Dev Diary&lt;\\\/&gt;\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/DALL\u00b7E-2025-02-15-17.42.49-A-minimalistic-and-illustrative-digital-artwork-representing-authentication-and-authorization-in-Spring-Security.-The-image-should-depict-a-secure-log.webp\",\"datePublished\":\"2025-03-15T05:55:00+00:00\",\"description\":\"Learn how to implement authentication and authorization in Spring Boot using Spring Security. Secure your application with JWT, OAuth2, ...\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/#primaryimage\",\"url\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/DALL\u00b7E-2025-02-15-17.42.49-A-minimalistic-and-illustrative-digital-artwork-representing-authentication-and-authorization-in-Spring-Security.-The-image-should-depict-a-secure-log.webp\",\"contentUrl\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/03\\\/DALL\u00b7E-2025-02-15-17.42.49-A-minimalistic-and-illustrative-digital-artwork-representing-authentication-and-authorization-in-Spring-Security.-The-image-should-depict-a-secure-log.webp\",\"width\":1024,\"height\":1024,\"caption\":\"Spring Security: Implementing Authentication & Authorization\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/spring-security-authentication-authorization-guide\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Spring Security: Implementing Authentication &amp; Authorization\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/\",\"name\":\"Harshad's Dev Diary\",\"description\":\"HARSHAD&#039;s Dev Diary\",\"publisher\":{\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/#\\\/schema\\\/person\\\/d82781218ba30c34fa81b49e8393681e\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/#\\\/schema\\\/person\\\/d82781218ba30c34fa81b49e8393681e\",\"name\":\"HS\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/about.jpg\",\"url\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/about.jpg\",\"contentUrl\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/about.jpg\",\"width\":400,\"height\":400,\"caption\":\"HS\"},\"logo\":{\"@id\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/wp-content\\\/uploads\\\/2025\\\/02\\\/about.jpg\"},\"sameAs\":[\"https:\\\/\\\/harshad-sonawane.com\\\/blog\"],\"url\":\"https:\\\/\\\/harshad-sonawane.com\\\/blog\\\/author\\\/admin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Security: Implementing Authentication &amp; Authorization - &lt;&gt;HARSHAD&#039;s Dev Diary&lt;\/&gt;","description":"Learn how to implement authentication and authorization in Spring Boot using Spring Security. Secure your application with JWT, OAuth2, ...","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/","og_locale":"en_US","og_type":"article","og_title":"Spring Security: Implementing Authentication &amp; Authorization - &lt;&gt;HARSHAD&#039;s Dev Diary&lt;\/&gt;","og_description":"Learn how to implement authentication and authorization in Spring Boot using Spring Security. Secure your application with JWT, OAuth2, ...","og_url":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/","og_site_name":"&lt;&gt;HARSHAD&#039;s Dev Diary&lt;\/&gt;","article_published_time":"2025-03-15T05:55:00+00:00","og_image":[{"width":1024,"height":1024,"url":"https:\/\/harshad-sonawane.com\/blog\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-02-15-17.42.49-A-minimalistic-and-illustrative-digital-artwork-representing-authentication-and-authorization-in-Spring-Security.-The-image-should-depict-a-secure-log.webp","type":"image\/webp"}],"author":"HS","twitter_card":"summary_large_image","twitter_misc":{"Written by":"HS","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/#article","isPartOf":{"@id":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/"},"author":{"name":"HS","@id":"https:\/\/harshad-sonawane.com\/blog\/#\/schema\/person\/d82781218ba30c34fa81b49e8393681e"},"headline":"Spring Security: Implementing Authentication &amp; Authorization","datePublished":"2025-03-15T05:55:00+00:00","mainEntityOfPage":{"@id":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/"},"wordCount":380,"commentCount":0,"publisher":{"@id":"https:\/\/harshad-sonawane.com\/blog\/#\/schema\/person\/d82781218ba30c34fa81b49e8393681e"},"image":{"@id":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/harshad-sonawane.com\/blog\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-02-15-17.42.49-A-minimalistic-and-illustrative-digital-artwork-representing-authentication-and-authorization-in-Spring-Security.-The-image-should-depict-a-secure-log.webp","keywords":["API Security","Authentication in Spring Boot","Authorization in Spring Boot","Java Security","JWT Authentication","Microservices Security","OAuth2 Spring Boot","Password Hashing","pring Security","RBAC Spring Security","Role-Based Access Control","Secure Spring Boot","Spring Boot Login","Spring Boot OAuth2","Spring Boot Security","Spring Security Best Practices","Spring Security JWT","Token-Based Authentication","Web Security"],"articleSection":["Java, Spring Boot, AWS, Cloud"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/","url":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/","name":"Spring Security: Implementing Authentication &amp; Authorization - &lt;&gt;HARSHAD&#039;s Dev Diary&lt;\/&gt;","isPartOf":{"@id":"https:\/\/harshad-sonawane.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/#primaryimage"},"image":{"@id":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/#primaryimage"},"thumbnailUrl":"https:\/\/harshad-sonawane.com\/blog\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-02-15-17.42.49-A-minimalistic-and-illustrative-digital-artwork-representing-authentication-and-authorization-in-Spring-Security.-The-image-should-depict-a-secure-log.webp","datePublished":"2025-03-15T05:55:00+00:00","description":"Learn how to implement authentication and authorization in Spring Boot using Spring Security. Secure your application with JWT, OAuth2, ...","breadcrumb":{"@id":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/#primaryimage","url":"https:\/\/harshad-sonawane.com\/blog\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-02-15-17.42.49-A-minimalistic-and-illustrative-digital-artwork-representing-authentication-and-authorization-in-Spring-Security.-The-image-should-depict-a-secure-log.webp","contentUrl":"https:\/\/harshad-sonawane.com\/blog\/wp-content\/uploads\/2025\/03\/DALL\u00b7E-2025-02-15-17.42.49-A-minimalistic-and-illustrative-digital-artwork-representing-authentication-and-authorization-in-Spring-Security.-The-image-should-depict-a-secure-log.webp","width":1024,"height":1024,"caption":"Spring Security: Implementing Authentication & Authorization"},{"@type":"BreadcrumbList","@id":"https:\/\/harshad-sonawane.com\/blog\/spring-security-authentication-authorization-guide\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/harshad-sonawane.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Spring Security: Implementing Authentication &amp; Authorization"}]},{"@type":"WebSite","@id":"https:\/\/harshad-sonawane.com\/blog\/#website","url":"https:\/\/harshad-sonawane.com\/blog\/","name":"Harshad's Dev Diary","description":"HARSHAD&#039;s Dev Diary","publisher":{"@id":"https:\/\/harshad-sonawane.com\/blog\/#\/schema\/person\/d82781218ba30c34fa81b49e8393681e"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/harshad-sonawane.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/harshad-sonawane.com\/blog\/#\/schema\/person\/d82781218ba30c34fa81b49e8393681e","name":"HS","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/harshad-sonawane.com\/blog\/wp-content\/uploads\/2025\/02\/about.jpg","url":"https:\/\/harshad-sonawane.com\/blog\/wp-content\/uploads\/2025\/02\/about.jpg","contentUrl":"https:\/\/harshad-sonawane.com\/blog\/wp-content\/uploads\/2025\/02\/about.jpg","width":400,"height":400,"caption":"HS"},"logo":{"@id":"https:\/\/harshad-sonawane.com\/blog\/wp-content\/uploads\/2025\/02\/about.jpg"},"sameAs":["https:\/\/harshad-sonawane.com\/blog"],"url":"https:\/\/harshad-sonawane.com\/blog\/author\/admin\/"}]}},"jetpack_publicize_connections":[],"_links":{"self":[{"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/posts\/53","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/comments?post=53"}],"version-history":[{"count":3,"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions"}],"predecessor-version":[{"id":142,"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/posts\/53\/revisions\/142"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/media\/74"}],"wp:attachment":[{"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/media?parent=53"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/categories?post=53"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/harshad-sonawane.com\/blog\/wp-json\/wp\/v2\/tags?post=53"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}