// Path: src/main/java/com/nuvola/myproject/server/security/AuthFailureHandler.java
// @Component
// public class AuthFailureHandler extends SimpleUrlAuthenticationFailureHandler {
//     @Override
//     public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
//             AuthenticationException exception) throws IOException, ServletException {
//         response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
// 
//         PrintWriter writer = response.getWriter();
//         writer.write(exception.getMessage());
//         writer.flush();
//     }
// }
// 
// Path: src/main/java/com/nuvola/myproject/server/security/AuthSuccessHandler.java
// @Component
// public class AuthSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
//     private static final Logger LOGGER = LoggerFactory.getLogger(AuthSuccessHandler.class);
// 
//     private final ObjectMapper mapper;
// 
//     @Autowired
//     AuthSuccessHandler(MappingJackson2HttpMessageConverter messageConverter) {
//         this.mapper = messageConverter.getObjectMapper();
//     }
// 
//     @Override
//     public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
//             Authentication authentication) throws IOException, ServletException {
//         response.setStatus(HttpServletResponse.SC_OK);
// 
//         NuvolaUserDetails userDetails = (NuvolaUserDetails) authentication.getPrincipal();
//         User user = userDetails.getUser();
//         userDetails.setUser(user);
// 
//         LOGGER.info(userDetails.getUsername() + " got is connected ");
// 
//         PrintWriter writer = response.getWriter();
//         mapper.writeValue(writer, user);
//         writer.flush();
//     }
// }
// 
// Path: src/main/java/com/nuvola/myproject/server/security/HttpAuthenticationEntryPoint.java
// @Component
// public class HttpAuthenticationEntryPoint implements AuthenticationEntryPoint {
//     @Override
//     public void commence(HttpServletRequest request, HttpServletResponse response,
//             AuthenticationException authException) throws IOException {
//         response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
//     }
// }
// 
// Path: src/main/java/com/nuvola/myproject/server/security/HttpLogoutSuccessHandler.java
// @Component
// public class HttpLogoutSuccessHandler implements LogoutSuccessHandler {
//     @Override
//     public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
//             throws IOException {
//         response.setStatus(HttpServletResponse.SC_OK);
//         response.getWriter().flush();
//     }
// }
// 
// Path: src/main/java/com/nuvola/myproject/server/security/NuvolaUserDetailsService.java
// @Service
// public class NuvolaUserDetailsService implements UserDetailsService {
//     private final UserService userService;
// 
//     @Autowired
//     NuvolaUserDetailsService(UserService userService) {
//         this.userService = userService;
//     }
// 
//     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//         User user = userService.getUserByUsername(username);
//         if (user == null) {
//             throw new UsernameNotFoundException(username);
//         }
// 
//         List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
//         List<String> permissions = userService.getPermissions(user.getLogin());
//         for (String permission : permissions) {
//             grantedAuthorities.add(new SimpleGrantedAuthority(permission));
//         }
// 
//         return new NuvolaUserDetails(user, grantedAuthorities);
//     }
// }
// 
// Path: src/main/java/com/nuvola/myproject/shared/Parameters.java
// public class Parameters {
//     public static final String USERNAME = "username";
//     public static final String PASSWORD = "password";
// }
// 
// Path: src/main/java/com/nuvola/myproject/shared/ResourcePaths.java
// public class ResourcePaths {
//     public class User {
//         public static final String ROOT = "/user";
//         public static final String LOGIN = "/login";
//     }
// }


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import com.nuvola.myproject.server.security.AuthFailureHandler;
import com.nuvola.myproject.server.security.AuthSuccessHandler;
import com.nuvola.myproject.server.security.HttpAuthenticationEntryPoint;
import com.nuvola.myproject.server.security.HttpLogoutSuccessHandler;
import com.nuvola.myproject.server.security.NuvolaUserDetailsService;
import com.nuvola.myproject.shared.Parameters;
import com.nuvola.myproject.shared.ResourcePaths;
package com.nuvola.myproject.server.spring;



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
@ComponentScan(value = "com.nuvola.**.security")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    private static final String LOGIN_PATH = ResourcePaths.User.ROOT + ResourcePaths.User.LOGIN;

    @Autowired
    private NuvolaUserDetailsService userDetailsService;
    @Autowired
    private HttpAuthenticationEntryPoint authenticationEntryPoint;
    @Autowired
// Path: src/main/java/com/nuvola/myproject/server/security/AuthFailureHandler.java
// @Component
// public class AuthFailureHandler extends SimpleUrlAuthenticationFailureHandler {
//     @Override
//     public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
//             AuthenticationException exception) throws IOException, ServletException {
//         response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
// 
//         PrintWriter writer = response.getWriter();
//         writer.write(exception.getMessage());
//         writer.flush();
//     }
// }
// 
// Path: src/main/java/com/nuvola/myproject/server/security/AuthSuccessHandler.java
// @Component
// public class AuthSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {
//     private static final Logger LOGGER = LoggerFactory.getLogger(AuthSuccessHandler.class);
// 
//     private final ObjectMapper mapper;
// 
//     @Autowired
//     AuthSuccessHandler(MappingJackson2HttpMessageConverter messageConverter) {
//         this.mapper = messageConverter.getObjectMapper();
//     }
// 
//     @Override
//     public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
//             Authentication authentication) throws IOException, ServletException {
//         response.setStatus(HttpServletResponse.SC_OK);
// 
//         NuvolaUserDetails userDetails = (NuvolaUserDetails) authentication.getPrincipal();
//         User user = userDetails.getUser();
//         userDetails.setUser(user);
// 
//         LOGGER.info(userDetails.getUsername() + " got is connected ");
// 
//         PrintWriter writer = response.getWriter();
//         mapper.writeValue(writer, user);
//         writer.flush();
//     }
// }
// 
// Path: src/main/java/com/nuvola/myproject/server/security/HttpAuthenticationEntryPoint.java
// @Component
// public class HttpAuthenticationEntryPoint implements AuthenticationEntryPoint {
//     @Override
//     public void commence(HttpServletRequest request, HttpServletResponse response,
//             AuthenticationException authException) throws IOException {
//         response.sendError(HttpServletResponse.SC_UNAUTHORIZED, authException.getMessage());
//     }
// }
// 
// Path: src/main/java/com/nuvola/myproject/server/security/HttpLogoutSuccessHandler.java
// @Component
// public class HttpLogoutSuccessHandler implements LogoutSuccessHandler {
//     @Override
//     public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
//             throws IOException {
//         response.setStatus(HttpServletResponse.SC_OK);
//         response.getWriter().flush();
//     }
// }
// 
// Path: src/main/java/com/nuvola/myproject/server/security/NuvolaUserDetailsService.java
// @Service
// public class NuvolaUserDetailsService implements UserDetailsService {
//     private final UserService userService;
// 
//     @Autowired
//     NuvolaUserDetailsService(UserService userService) {
//         this.userService = userService;
//     }
// 
//     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
//         User user = userService.getUserByUsername(username);
//         if (user == null) {
//             throw new UsernameNotFoundException(username);
//         }
// 
//         List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
//         List<String> permissions = userService.getPermissions(user.getLogin());
//         for (String permission : permissions) {
//             grantedAuthorities.add(new SimpleGrantedAuthority(permission));
//         }
// 
//         return new NuvolaUserDetails(user, grantedAuthorities);
//     }
// }
// 
// Path: src/main/java/com/nuvola/myproject/shared/Parameters.java
// public class Parameters {
//     public static final String USERNAME = "username";
//     public static final String PASSWORD = "password";
// }
// 
// Path: src/main/java/com/nuvola/myproject/shared/ResourcePaths.java
// public class ResourcePaths {
//     public class User {
//         public static final String ROOT = "/user";
//         public static final String LOGIN = "/login";
//     }
// }


// Path: src/main/java/com/nuvola/myproject/server/spring/WebSecurityConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import com.nuvola.myproject.server.security.AuthFailureHandler;
import com.nuvola.myproject.server.security.AuthSuccessHandler;
import com.nuvola.myproject.server.security.HttpAuthenticationEntryPoint;
import com.nuvola.myproject.server.security.HttpLogoutSuccessHandler;
import com.nuvola.myproject.server.security.NuvolaUserDetailsService;
import com.nuvola.myproject.shared.Parameters;
import com.nuvola.myproject.shared.ResourcePaths;

package com.nuvola.myproject.server.spring;



@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true)
@ComponentScan(value = "com.nuvola.**.security")
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    private static final String LOGIN_PATH = ResourcePaths.User.ROOT + ResourcePaths.User.LOGIN;

    @Autowired
    private NuvolaUserDetailsService userDetailsService;
    @Autowired
    private HttpAuthenticationEntryPoint authenticationEntryPoint;
    @Autowired
    private AuthSuccessHandler authSuccessHandler;