Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.project.springinventory.configuration;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
- 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.crypto.bcrypt.BCryptPasswordEncoder;
- @Configuration
- @EnableWebSecurity
- public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
- @Autowired
- @Qualifier("UsersService")
- private UserDetailsService userService;
- @Autowired
- public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
- auth.userDetailsService(userService).passwordEncoder(new BCryptPasswordEncoder());
- }
- @Override
- protected void configure(HttpSecurity http) throws Exception {
- // TODO Auto-generated method stub
- http.authorizeRequests().antMatchers("/css/*,/imgs/*").permitAll().and().formLogin().loginPage("/login").loginProcessingUrl("/loginCheck").usernameParameter("username").passwordParameter("password").defaultSuccessUrl("/customermanagement").permitAll().and().logout().logoutUrl("/logout").permitAll();
- super.configure(http);
- }
- }
- package com.project.springinventory.controller;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.ModelAttribute;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
- import com.project.springinventory.constant.ViewConstant;
- import com.project.springinventory.entity.User;
- @Controller
- @RequestMapping("/")
- public class MainIndexController {
- @GetMapping("/")
- public ModelAndView ViewPost(@ModelAttribute("EntityUser") User entityUser) {
- ModelAndView mav = new ModelAndView();
- // @Valid @ModelAttribute("EntityUser") User entityUser ,BindingResult
- // bindingResult
- // System.err.println("User: " + entityUser.getUsername() + " Pass: " +
- // entityUser.getPassword());
- // if (bindingResult.hasErrors()) {
- // mav.setViewName(ViewConstant.LOGINERROR_VIEW);
- // } else {
- // if (entityUser.getUsername().equalsIgnoreCase("admin") &&
- // entityUser.getPassword().equalsIgnoreCase("admin")) {
- // ViewConstant.REDIRECT_CONSTANT +
- mav.setViewName(ViewConstant.LOGIN_VIEW);
- // mav.addObject("EntityUser", entityUser);
- // } else {
- // mav.setViewName(ViewConstant.LOGINERROR_VIEW);
- // mav.addObject("EntityUser", new User());
- // }
- // }
- return mav;
- }
- }
- <!DOCTYPE html>
- <html lang="en-US" xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8" />
- <title>Acceso</title>
- </head>
- <body>
- <div class="login-card">
- <h1>
- <span th:text="${name}"></span> <img class="candado"
- th:src="@{/img/candado.png}" src="/static/img/candado.png" />
- </h1>
- <br />
- <form th:action="@{/login}"
- method="POST">
- <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
- <input type="text" name="username" placeholder="Username" id="username"/>
- <!-- <p th:if="${#fields.hasErrors('username')}" th:errors="*{username}">Username -->
- <!-- has errors</p> -->
- <input type="password" name="pass" placeholder="Password" id="password"/>
- <!-- <p th:if="${#fields.hasErrors('password')}" th:errors="*{password}">Password -->
- <!-- has errors</p> -->
- <input type="submit" name="login" class="btn"
- value="Acceder" />
- </form>
- <div class="login-help">
- <a href="#">Forgot Password</a>
- </div>
- </div>
- </body>
- <link rel="stylesheet" th:href="@{/css/jquery-ui.css}"
- href="/static/css/jquery-ui.css" />
- <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}"
- href="/static/css/bootstrap.min.css" />
- <link rel="stylesheet" th:href="@{/css/style.css}"
- href="/static/css/style.css" />
- </html>
Add Comment
Please, Sign In to add comment