{"id":3927,"date":"2020-09-28T16:01:22","date_gmt":"2020-09-28T16:01:22","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=3927"},"modified":"2020-09-29T15:47:43","modified_gmt":"2020-09-29T15:47:43","slug":"spring-security-basics","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/spring-security-basics\/","title":{"rendered":"Spring Security Basics"},"content":{"rendered":"<p><strong class=\"ctaHeader2\">5 Core Concepts of Spring Security<\/strong><\/p>\n<ol>\n<li><strong>Authentication and Authorization<\/strong><br \/>\n   &#8211; Authentication &#8211; Who are you &#8211; Answer by showing ID(Facebook, LinkedIn for ID which uniquely identifies you)<br \/>\n   &#8211; Authorization  &#8211; What you want &#8211; State what you want   <\/p>\n<p>\tKnowledge Based Authentication &#8211; Providing details you know about you to prove its you. Downside is details can be stolen.<br \/>\n\tPossession Based Authentication &#8211; Key Cards for accessing Building Doors, Phone OTP. Authenticates by checking the user posses something which<br \/>\n\t\t\t\t\t\t\t\t\t  realuser should posses.  <\/p>\n<p>\tMulti Factor Authentication &#8211; Enter password and enter OTP(KBA + PBA)      <\/li>\n<li><strong>Authorization <\/strong>&#8211; Checks whether the person is allowed to do something. For Authorization, Authentication is needed at first place.<\/li>\n<li> <strong>Principal <\/strong><br \/>\n   &#8211; Person identified through process of Authentication<br \/>\n   &#8211; Person who has logged in. Currently logged in user (or) account.<br \/>\n   &#8211; App remembers the principal in context as currently loggedin user.<\/li>\n<li> <strong>Granted Authority<\/strong><br \/>\n    &#8211; Authority includes whether the user is allowed to Read, Write, Update and Delete at permission level\t<\/li>\n<li><strong>Role <\/strong><br \/>\n    &#8211; Group of Authorities assigned together forms a role<\/li>\n<\/ol>\n<p><strong>Formbased Authentication<\/strong><br \/>\n<strong>pom.xml<\/strong><\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n.\r\n.\r\n&lt;dependency&gt;\r\n  &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n  &lt;artifactId&gt;spring-boot-starter-security&lt;\/artifactId&gt;\r\n&lt;\/dependency&gt;\r\n.\r\n.\r\n<\/pre>\n<p><strong class=\"ctaHeader3\">Basic Auth<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2020\/09\/Basic-Auth.png\" alt=\"null\" \/><\/p>\n<ol>\n<li>Client Sends a request without username and password and gets 401 Unauthorized as Response<\/li>\n<li>Now Client Sends a request with username and password with Base64 Encoding<\/li>\n<li>Server validates whether user exists in DB<\/li>\n<li>Server replies with 200 Ok if user authentication is successful<\/li>\n<li>Basic ENCODED-BASE64-USERIDPASSWORD is the one sent in header to server from client<\/li>\n<li>In postman basic auth can be done by adding Authorization and base64 encoded user and password to header\n<pre>\r\nHeader : Authorization\r\nValue : Basic base64('YourOrgName:YourAPIKEY');\r\n<\/pre>\n<\/li>\n<li>\nBase64 encoded text can be got from JS Console in browser as below<\/p>\n<pre>\r\n\"username:password!\" \/\/ Here I used basic Auth string format\r\n\r\n\/\/ Encode the plain string to base64\r\nbtoa(\"username:password!\"); \/\/ output: \"dXNlcm5hbWU6cGFzc3dvcmQh\"\r\n\r\n\r\n\/\/ Decode the base64 to plain string\r\natob(\"dXNlcm5hbWU6cGFzc3dvcmQh\"); \/\/ output: \"username:password!\"\r\n<\/pre>\n<\/li>\n<li>Using Authorization Tab in post man does the same thing of adding base64 encoded UserName and Password to Header prepending Basic<\/li>\n<\/ol>\n<p><strong>The Difference between FormAuth and BasicAuth is in BasicAuth UserName and Password would be sent everytime when making a request to the server in the header as base64 encoded character.<\/strong><\/p>\n<p><strong class=\"ctaHeader3\">Form-based authentication <\/strong><br \/>\nForm-based authentication is not formalized by any RFC.They don\u2019t use the formal HTTP authentication techniques.They use the standard HTML form fields to pass the username and password values to the server.The server validates the credentials and then creates a \u201csession\u201d that is tied to a unique key that is passed between the client and server on each http put and get request.When the user clicks \u201clog off\u201d or the server logs the user off (for example after certain idle time), the server will invalidate the session key, which makes any subsequent communication between the client and server require re-validation<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2020\/09\/FormBasedAuth.jpg\" alt=\"null\" \/><\/p>\n<p><strong>Basic Auth<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2020\/09\/BasicAuth1.jpg\" alt=\"null\" \/><\/p>\n<p><strong>Basic Auth with Authorization in Headers as seen in DevTool<\/strong><br \/>\n<img decoding=\"async\" src=\"https:\/\/codethataint.com\/blog\/wp-content\/uploads\/2020\/09\/BasicAuth2.jpg\" alt=\"null\" \/><\/p>\n<p>Creating the below class in Spring Boot project would enable the Basic auth(httpAuth) instead of default formbased auth which we get after adding spring security starter dependency to pom.xml<\/p>\n<p><strong>ApplicationSecurityConfig.java<\/strong><br \/>\n<em>Using Custom Username and Password for Inmemory Authentication<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Configuration\r\n@EnableWebSecurity\r\npublic class ApplicationSecurityConfig extends WebSecurityConfigurerAdapter {\r\n    @Override\r\n    protected void configure(HttpSecurity httpSecurity) throws Exception{\r\n        httpSecurity.authorizeRequests()\r\n                    .anyRequest()\r\n                    .authenticated()\r\n                    .and()\r\n                    .httpBasic();\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>5 Core Concepts of Spring Security Authentication and Authorization &#8211; Authentication &#8211; Who are you &#8211; Answer by showing ID(Facebook, LinkedIn for ID which uniquely identifies you) &#8211; Authorization &#8211; What you want &#8211; State what you want Knowledge Based Authentication &#8211; Providing details you know about you to prove its you. Downside is details&hellip; <a href=\"https:\/\/codethataint.com\/blog\/spring-security-basics\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[325],"tags":[],"class_list":["post-3927","post","type-post","status-publish","format-standard","hentry","category-spring-security"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3927","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3927"}],"version-history":[{"count":5,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3927\/revisions"}],"predecessor-version":[{"id":3951,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3927\/revisions\/3951"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3927"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3927"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3927"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}