<repo_name>khamulbr/books-ws<file_sep>/src/main/java/com/nazgulsoft/apis/booksws/security/UserDetailsServiceImpl.java
package com.nazgulsoft.apis.booksws.security;

import com.nazgulsoft.apis.booksws.user.User;
import com.nazgulsoft.apis.booksws.user.UserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    private UserService userService;

    public UserDetailsServiceImpl(UserService userService) {
        this.userService = userService;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getUserByUsername(username);
        if (user != null)
            return user;
        throw new UsernameNotFoundException(username + " does not exist");
    }
}
<file_sep>/src/main/resources/application.properties
server.ssl.key-store=serverkeystore.jks
server.ssl.key-store-type=JKS
server.ssl.key-store-password=<PASSWORD>
server.ssl.key-alias=tcserver
<file_sep>/README.md
# books-ws
Code for Spring security code
<file_sep>/src/test/java/com/nazgulsoft/apis/booksws/book/BookControllerIT.java
package com.nazgulsoft.apis.booksws.book;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@AutoConfigureMockMvc
@ContextConfiguration(classes = BookController.class)
@WebMvcTest
class BookControllerIT {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @BeforeEach
    public void setup() {
        mockMvc = MockMvcBuilders
                .webAppContextSetup(context)
                .apply(springSecurity())
                .build();
    }

    @WithMockUser("user")
    @Test
    public void testGetBookById() throws Exception {
        MvcResult result = mockMvc.perform(MockMvcRequestBuilders.get("/v1/books/1234"))
                .andExpect(status().isOk())
                .andReturn();

        System.out.println(result.getResponse().getContentAsString());
    }

}<file_sep>/src/test/java/com/nazgulsoft/apis/booksws/user/UserServiceTest.java
package com.nazgulsoft.apis.booksws.user;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;


class UserServiceTest {

    private UserService userService = new UserService();

    @Test
    public void itShouldReturnTheProperUser__WhenAUserIsPresent() {
        //Given
        User expectedUser = new User("myusername", "$2a$12$5rYnmiswPytMCmenD7BX6OmeYBe1jYFfKP3B5vG5d80K6gkr5Xr6C", true);

        //When
        User actualUser = userService.getUserByUsername("myusername");

        //Then
        assertEquals(expectedUser, actualUser);
    }

    @Test
    public void itShouldReturnNull__WhenAUserIsNotPresent() {
        //Given
        User expectedUser = new User();

        //When
        User actualUser = userService.getUserByUsername("myusername2");

        //Then
        assertNull(actualUser);
    }

}<file_sep>/src/main/java/com/nazgulsoft/apis/booksws/user/User.java
package com.nazgulsoft.apis.booksws.user;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.Objects;

public class User implements UserDetails {

    private String username;
    private String password;
    private Boolean enabled;

    public User() {
    }

    public User(String username, String password, Boolean enabled) {
        this.username = username;
        this.password = <PASSWORD>;
        this.enabled = enabled;
    }

    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return enabled;
    }

    @Override
    public boolean isAccountNonLocked() {
        return enabled;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return enabled;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
    }

    public String getPassword() {
        return <PASSWORD>;
    }

    public void setPassword(String password) {
        this.password = <PASSWORD>;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return username.equals(user.username) &&
                password.equals(user.password) &&
                enabled.equals(user.enabled);
    }

    @Override
    public int hashCode() {
        return Objects.hash(username, password, enabled);
    }
}
