<?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>DEV Community: Ajinkya. Shivpure</title>
    <description>The latest articles on DEV Community by Ajinkya. Shivpure (@ajinkya_shivpure).</description>
    <link>https://dev.to/ajinkya_shivpure</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%2F2838371%2Fe3337e2c-831d-461f-be3d-f66983c8113e.png</url>
      <title>DEV Community: Ajinkya. Shivpure</title>
      <link>https://dev.to/ajinkya_shivpure</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ajinkya_shivpure"/>
    <language>en</language>
    <item>
      <title>Building User Authentication in Spring Boot: From Signup to Login</title>
      <dc:creator>Ajinkya. Shivpure</dc:creator>
      <pubDate>Tue, 11 Feb 2025 16:49:55 +0000</pubDate>
      <link>https://dev.to/ajinkya_shivpure/building-user-authentication-in-spring-boot-from-signup-to-login-2h1c</link>
      <guid>https://dev.to/ajinkya_shivpure/building-user-authentication-in-spring-boot-from-signup-to-login-2h1c</guid>
      <description>&lt;p&gt;After setting up our first Spring Boot project (check my previous post!), let's add user authentication. I'll show you how I implemented signup and login functionality using Spring Boot's layered architecture.&lt;/p&gt;

&lt;h3&gt;
  
  
  Project Structure:
&lt;/h3&gt;

&lt;p&gt;Now , as a beginner even keeping a proper structure for your project can be difficult . create proper packages and classes. here is the project structure you should use :&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7h1m9rjv591wlmh7btt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe7h1m9rjv591wlmh7btt.png" alt="Intelli J" width="800" height="1574"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fep5udbimv6gph2asz3r0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fep5udbimv6gph2asz3r0.png" alt="structure " width="800" height="542"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  1.The User Entity:
&lt;/h2&gt;

&lt;p&gt;(we created this earlier as well ):&lt;br&gt;
this user entity class stores all the attributes of a user like name , email and password .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    private String name;

    @Column(unique = true, nullable = false)
    private String email;

    @Column(nullable = false)
    private String password;  // Will be stored encoded
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. User Repository:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm0n30v37zwj61fy4ln16.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fm0n30v37zwj61fy4ln16.png" alt="layers" width="800" height="829"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Refer the above diagram for better clarification . the database layer is basically the repository . here , the user repository is responsible for connecting with our my sql database . it has complete access to the user table . we are using the JpaRepository for some built in features .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Repository
public interface UserRepository extends JpaRepository&amp;lt;User, Long&amp;gt; {
    Optional&amp;lt;User&amp;gt; findByEmail(String email);
    Boolean existsByEmail(String email);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  3. DTOs - for request and response
&lt;/h2&gt;

&lt;p&gt;we use this for convenience. you will understand as you proceed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class SignupRequest {
    private String name;
    private String email;
    private String password;
    // getters, setters
}

public class LoginRequest {
    private String email;
    private String password;
    // getters, setters
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Auth Service :
&lt;/h2&gt;

&lt;p&gt;the work of the service layer is to receive request from api layer , process that request. the api layer does not communicate with the data access layers directly . see the api controller layer further below , it just contains certain endpoints then in each method , it calls the methods in service layer . &lt;br&gt;
we create a repository object in services and service object in controllers . that's how it works!&lt;br&gt;
I am not explaining much from this code , most of this is basic to moderate Java .&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Service
@Transactional
public class AuthService {
    @Autowired
    private UserRepository userRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    public User signup(SignupRequest request) {
        // Check if user exists
        if (userRepository.existsByEmail(request.getEmail())) {
            throw new RuntimeException("Email already registered");
        }

        // Create new user
        User user = new User();
        user.setName(request.getName());
        user.setEmail(request.getEmail());
        user.setPassword(passwordEncoder.encode(request.getPassword()));

        return userRepository.save(user);
    }

    public User login(LoginRequest request) {
        User user = userRepository.findByEmail(request.getEmail())
            .orElseThrow(() -&amp;gt; new RuntimeException("User not found"));

        if (!passwordEncoder.matches(request.getPassword(), user.getPassword())) {
            throw new RuntimeException("Invalid password");
        }

        return user;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  @ Autowired makes the work easier , instead of initiating the object in constructor just use the annotation @autowired , it automatically tells springboot what we need .
&lt;/h3&gt;

&lt;h2&gt;
  
  
  5. Auth Controller :
&lt;/h2&gt;

&lt;p&gt;let me explain the important key components :&lt;br&gt;
@RequestMapping: it tells that all the requests will be sent on "/api/auth" . you can customise it . the default path for requests is &lt;br&gt;
&lt;a href="http://localhost:8080/api/auth" rel="noopener noreferrer"&gt;http://localhost:8080/api/auth&lt;/a&gt; . by requests , I mean the ones from frontend .&lt;br&gt;
@PostMapping:&lt;br&gt;
it tells that the tor of request is the post request , that is some data is going to be submitted to the repository . now , the complete path becomes &lt;br&gt;
&lt;a href="http://localhost:8080/api/auth/signup" rel="noopener noreferrer"&gt;http://localhost:8080/api/auth/signup&lt;/a&gt; for sign up and so on&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@RestController
@RequestMapping("/api/auth")
public class AuthController {
    @Autowired
    private AuthService authService;

    @PostMapping("/signup")
    public ResponseEntity&amp;lt;?&amp;gt; signup(@RequestBody SignupRequest request) {
        try {
            User user = authService.signup(request);
            return ResponseEntity.ok("User registered successfully");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @PostMapping("/login")
    public ResponseEntity&amp;lt;?&amp;gt; login(@RequestBody LoginRequest request) {
        try {
            User user = authService.login(request);
            return ResponseEntity.ok("Login successful");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Key Learning Points:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Always encode passwords before storing.&lt;/li&gt;
&lt;li&gt;Use DTOs to handle request/response data.&lt;/li&gt;
&lt;li&gt;Implement proper validation and error handling.&lt;/li&gt;
&lt;li&gt;Follow separation of concerns (Controller → Service → Repository).&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;Now , we need to test these apis. I use postman for this purpose . I will recommend for all of you to go with it . &lt;/p&gt;

&lt;h3&gt;
  
  
  PostMan testing:
&lt;/h3&gt;

&lt;p&gt;the following image should be enough .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqv2m3tid2lq3sv0xw4d4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqv2m3tid2lq3sv0xw4d4.png" alt="postman" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Coming Up Next:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Adding JWT token authentication&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Implementing role-based authorization&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h1&gt;
  
  
  springboot #java #security #authentication #webdev
&lt;/h1&gt;

</description>
    </item>
    <item>
      <title>Getting Started with Spring Boot: A Beginner's Journey</title>
      <dc:creator>Ajinkya. Shivpure</dc:creator>
      <pubDate>Sun, 09 Feb 2025 17:43:27 +0000</pubDate>
      <link>https://dev.to/ajinkya_shivpure/getting-started-with-spring-boot-a-beginners-journey-391k</link>
      <guid>https://dev.to/ajinkya_shivpure/getting-started-with-spring-boot-a-beginners-journey-391k</guid>
      <description>&lt;p&gt;As a Java developer starting my journey with Spring Boot, I want to share my experience setting up my first project. This guide will walk you through the essential steps, from project creation to implementing your first entity.&lt;/p&gt;

&lt;p&gt;Why I Chose Spring Boot?&lt;br&gt;
Spring Boot simplifies the development of stand-alone, production-grade Spring applications. It takes an opinionated view of the Spring platform, which means less boilerplate code and faster development.&lt;/p&gt;
&lt;h3&gt;
  
  
  Prerequisites:
&lt;/h3&gt;

&lt;p&gt;1.)JDK 23 &lt;br&gt;
2.)IntelliJ IDEA Ultimate Edition - I used student membership&lt;br&gt;
3.)Basic understanding of Java like basic syntax, if -else loops,oops concepts..etc&lt;br&gt;
4.)MySQL installed on your system&lt;/p&gt;


&lt;h2&gt;
  
  
  Creating Your First Project
&lt;/h2&gt;
&lt;h1&gt;
  
  
  Step 1: Using Spring Initializer
&lt;/h1&gt;

&lt;p&gt;Navigate to start.spring.io&lt;br&gt;
Configure your project:&lt;/p&gt;

&lt;p&gt;Project: Maven&lt;br&gt;
Language: Java&lt;br&gt;
Spring Boot Version: 3.4.2 (Latest Stable Version) as of now .&lt;br&gt;
Group: com.example&lt;br&gt;
Artifact: demo&lt;br&gt;
Packaging: Jar&lt;br&gt;
Java Version: 23&lt;/p&gt;

&lt;p&gt;Step 2: Adding Essential Dependencies&lt;br&gt;
I added these dependencies for a basic web application with database support:&lt;/p&gt;

&lt;p&gt;Spring Web: For building RESTful applications&lt;br&gt;
Spring Data JPA: For database operations&lt;br&gt;
MySQL Driver: To connect with MySQL database&lt;br&gt;
Spring Security: For authentication and authorization (optional)&lt;/p&gt;

&lt;p&gt;Pro tip: Hover over each dependency in Spring Initializer to understand its purpose!&lt;/p&gt;


&lt;h2&gt;
  
  
  Understanding Project Structure
&lt;/h2&gt;

&lt;p&gt;The pom.xml File&lt;br&gt;
The pom.xml is your project's configuration file. Here's what mine looked like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;project xmlns="http://maven.apache.org/POM/4.0.0"&amp;gt;
    &amp;lt;groupId&amp;gt;com.example&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;demo&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
    &amp;lt;name&amp;gt;demo&amp;lt;/name&amp;gt;
    &amp;lt;description&amp;gt;Demo project for Spring Boot&amp;lt;/description&amp;gt;

    &amp;lt;dependencies&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-boot-starter-web&amp;lt;/artifactId&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-boot-starter-data-jpa&amp;lt;/artifactId&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;mysql&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;mysql-connector-java&amp;lt;/artifactId&amp;gt;
        &amp;lt;/dependency&amp;gt;
    &amp;lt;/dependencies&amp;gt;
&amp;lt;/project&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configuring application.properties
&lt;/h2&gt;

&lt;p&gt;This file contains your application's configuration. Here's my basic setup for MySQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring.datasource.url=jdbc:mysql://localhost:3306/your_database_name
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Creating Your First Entity
&lt;/h2&gt;

&lt;p&gt;I created a simple User entity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Entity
@Table(name = "users")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;

    @Column(unique = true)
    private String email;

    private String password;

    // Getters and Setters
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Key Points About the Entity:&lt;/p&gt;

&lt;p&gt;1.@Entity: Marks this class as a JPA entity.&lt;br&gt;
2.@Table: Specifies the table name in database.&lt;br&gt;
3.&lt;a class="mentioned-user" href="https://dev.to/id"&gt;@id&lt;/a&gt;: Marks the primary key&lt;br&gt;
4.@GeneratedValue: Configures auto-increment&lt;br&gt;
5.&lt;a class="mentioned-user" href="https://dev.to/column"&gt;@column&lt;/a&gt;(unique = true): Ensures email uniqueness&lt;/p&gt;




&lt;h3&gt;
  
  
  Running the Application:
&lt;/h3&gt;

&lt;p&gt;1.Open the project in IntelliJ IDEA&lt;br&gt;
2.Wait for Maven to download dependencies&lt;br&gt;
3.Run the main application class (has 4.@SpringBootApplication annotation)&lt;br&gt;
5.Check the console for "Started Application" message&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fznsi87xav7l1fvye3vac.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fznsi87xav7l1fvye3vac.png" alt="This is the message shown by the console" width="800" height="172"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Common Issues I Faced:
&lt;/h3&gt;

&lt;p&gt;1.Database connection errors: Double-check your MySQL service is running&lt;br&gt;
2.Port conflicts: Default port is 8080, change it in application.properties if needed&lt;br&gt;
3.Maven dependencies not downloading: Check your internet connection and Maven settings. reload the project . IntelliJ basically will help you in such things . &lt;/p&gt;




&lt;h3&gt;
  
  
  Next Steps:
&lt;/h3&gt;

&lt;p&gt;Create a Repository interface for database operations&lt;br&gt;
Build REST controllers&lt;br&gt;
Add service layer business logic&lt;br&gt;
Implement basic CRUD operations&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Setting up a Spring Boot project might seem overwhelming at first, but once you understand the basic structure, it becomes much clearer. In my next post, I'll cover how to implement CRUD operations with our User entity.&lt;br&gt;
Happy coding!&lt;/p&gt;




&lt;p&gt;Follow me for more Spring Boot tutorials and Java development insights!&lt;/p&gt;

&lt;h1&gt;
  
  
  java #springboot #programming #webdevelopment #tutorial
&lt;/h1&gt;

</description>
    </item>
  </channel>
</rss>
