Commit a941d786 authored by ourfbht's avatar ourfbht
Browse files

#1 changed the jpa system

parent ecfbc496
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -39,6 +39,10 @@
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.session</groupId>
			<artifactId>spring-session-core</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
+0 −9
Original line number Diff line number Diff line
package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@@ -13,10 +10,4 @@ public class MVCController implements WebMvcConfigurer {
        registry.addViewController("/login").setViewName("login");
    }

    /*@Bean
    public BCryptPasswordEncoder passwordEncoder(){
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        return bCryptPasswordEncoder;
    }*/

}
+18 −20
Original line number Diff line number Diff line
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
@@ -8,9 +9,9 @@ import org.springframework.security.config.annotation.authentication.builders.Au
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;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
@@ -19,11 +20,19 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private CustomLoginSuccessHandler successHandler;

    @Qualifier("userDetailsServiceImpl")
	@Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;
	private UserDetailsService userDetailsService;

    @Autowired
    private DataSource dataSource;
    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public AuthenticationManager customAuthenticationManager() throws Exception {
        return authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
@@ -43,12 +52,10 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                .antMatchers("/admin/**").hasAnyAuthority("ADMIN")
                .antMatchers("/member/**").hasAnyAuthority("POULAIN", "MENTOR")
                .and()
            .csrf().disable().formLogin()
                .loginPage("/login")
                .failureUrl("/login?error=true")
                .successHandler(successHandler)
            .formLogin()
                .loginPage("/login").permitAll()
                .usernameParameter("email")
                .passwordParameter("password")
                .successHandler(successHandler)
                .and()
            .logout()
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
@@ -57,18 +64,9 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
                .accessDeniedPage("/access-denied");
    }

    @Bean
    public AuthenticationManager customAuthenticationManager() throws Exception {
        return authenticationManager();
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.jdbcAuthentication().usersByUsernameQuery(
            "select email, password, '1' as enabled from auth_user where email=? and status='VERIFIED'"
        ).authoritiesByUsernameQuery(
            "select u.email, r.role_name from auth_user u inner join auth_user_role ur on(u.auth_user_id=ur.auth_user_id) inner join auth_role r on(ur.auth_role_id=r.auth_role_id) where u.email=?"
        ).dataSource(dataSource).passwordEncoder(bCryptPasswordEncoder);
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }

}
 No newline at end of file
+49 −33
Original line number Diff line number Diff line
package com.example.demo.controller;

import java.security.Principal;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
@@ -11,20 +14,15 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import com.example.demo.service.UserServiceInterface;

@Controller
public class AuthenticationController {
public class UserController {

    @Autowired
    UserService userService;
    private UserServiceInterface userService;

    
    @RequestMapping(value = { "/login" }, method = RequestMethod.GET)
    public ModelAndView login() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login"); // resources/template/login.html
        return modelAndView;
    }

    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public ModelAndView register() {
@@ -35,10 +33,49 @@ public class AuthenticationController {
        return modelAndView;
    }

    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public ModelAndView index() {
    @RequestMapping(value="/register", method=RequestMethod.POST)
    public ModelAndView registerUser(@Valid User user, @RequestParam("roleWanted") String roleWanted, BindingResult bindingResult, ModelMap modelMap) {
        ModelAndView modelAndView = new ModelAndView();

        //userSignupValidator.validate(o, errors);
        
        if(bindingResult.hasErrors()) { 
            modelAndView.addObject("registerMessage", "Registration failed: correct the fields !");
            modelMap.addAttribute("bindingResult", bindingResult);
        }
        else { // Saving the users
            userService.save(user, roleWanted);
            //securityService.autoLogin(user.getEmail(), user.getPassword());
            return new ModelAndView("redirect:" + "/");
        }

        modelAndView.addObject("user", new User());
        modelAndView.setViewName("register");
        
        return modelAndView;
    }

    @RequestMapping(value = { "/login" }, method = RequestMethod.GET)
    public ModelAndView login(HttpSession session, Principal principal) {
        
        if(principal != null){
            User user = userService.findByEmail(principal.getName());
            session.setAttribute("username", user.getUsername());
            session.setAttribute("id", user.getId());
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("login"); // resources/template/login.html
        return modelAndView;
    }

    @RequestMapping(value = {"/index", "", "/"}, method = RequestMethod.GET)
    public ModelAndView index(HttpSession session, Principal principal) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("index"); // resources/template/index.html
        String email = SecurityContextHolder.getContext().getAuthentication().getName();
        User user = userService.findByEmail(email);
        if (user != null)
            session.setAttribute("username", user.getUsername());
        return modelAndView;
    }
    
@@ -56,25 +93,4 @@ public class AuthenticationController {
        return modelAndView;
    }

    @RequestMapping(value="/register", method=RequestMethod.POST)
    public ModelAndView registerUser(@Valid User user, @RequestParam("roleWanted") String roleWanted, BindingResult bindingResult, ModelMap modelMap) {
        ModelAndView modelAndView = new ModelAndView();
        
        if(bindingResult.hasErrors()) { // Check for the validations
            modelAndView.addObject("registerMessage", "Registration failed: correct the fields !");
            modelMap.addAttribute("bindingResult", bindingResult);
        }
        else if(userService.isUserAlreadyPresent(user)){ // Checking if email already taken
            modelAndView.addObject("registerMessage", "User with this email already exist !");
        }
        else { // Saving the users
            userService.save(user, roleWanted);
            return new ModelAndView("redirect:" + "/");
        }

        modelAndView.addObject("user", new User());
        modelAndView.setViewName("register");
        
        return modelAndView;
    }
}
+5 −13
Original line number Diff line number Diff line
@@ -16,7 +16,7 @@ public class Role {
    private int id;

    @Column(name = "role_name")
    private String role;
    private String name;

    @Column(name = "role_desc")
    private String desc;
@@ -29,20 +29,12 @@ public class Role {
        this.id = id;
    }

    public String getRole() {
        return role;
    public String getName(){
        return name;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    public void setName(String name){
        this.name = name;
    }
    
    
Loading