Commit 9415f63c by Bogdan Andjelkovic

register controller and config

parent 1d7914a5
package com.example.SkuciSe.configuration;
import com.example.SkuciSe.model.korisnik.KorisnikDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
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 WebSecurityConfig extends WebSecurityConfigurerAdapter
{
@Bean
public UserDetailsService userDetailsService() {
return new KorisnikDetailsService();
}
@Bean
public BCryptPasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authenticationProvider = new DaoAuthenticationProvider();
authenticationProvider.setUserDetailsService( this.userDetailsService());
authenticationProvider.setPasswordEncoder( this.passwordEncoder());
return authenticationProvider;
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.authenticationProvider( authenticationProvider());
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/index").authenticated()
.anyRequest().permitAll()
.and().formLogin()
.usernameParameter("email")
.defaultSuccessUrl("/index")
.permitAll()
.and()
.logout().logoutSuccessUrl("/").permitAll();
}
}
package com.example.SkuciSe.controller;
import com.example.SkuciSe.model.korisnik.Korisnik;
import com.example.SkuciSe.repository.KorisnikRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class AppController
{
@Autowired
KorisnikRepository kRepo;
@GetMapping({"/","","/index"})
public String getIndex( Model model )
{
return("index");
}
@GetMapping("/login")
public String getLogin( Model model )
{
model.addAttribute("newUser", new Korisnik());
return("login");
}
@GetMapping("/register")
public String getRegister( Model model )
{
return("register");
}
@PostMapping("/register-proccess")
public String postRegisterProccess(@ModelAttribute Korisnik korisnik)
{
kRepo.insert( korisnik);
return("redirect:/login");
}
}
package com.example.SkuciSe.model; package com.example.SkuciSe.model.korisnik;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
...@@ -18,16 +18,14 @@ public class Korisnik ...@@ -18,16 +18,14 @@ public class Korisnik
String sifra; String sifra;
String telefon; String telefon;
int tipId; int tipId;
String slika;
public Korisnik(String ime, String prezime, String email, String sifra, String telefon, int tipId, String slika) { public Korisnik(String ime, String prezime, String email, String sifra, String telefon, int tipId) {
this.ime = ime; this.ime = ime;
this.prezime = prezime; this.prezime = prezime;
this.email = email; this.email = email;
this.sifra = sifra; this.sifra = sifra;
this.telefon = telefon; this.telefon = telefon;
this.tipId = tipId; this.tipId = tipId;
this.slika = slika;
} }
@Override @Override
...@@ -40,7 +38,6 @@ public class Korisnik ...@@ -40,7 +38,6 @@ public class Korisnik
", sifra='" + sifra + '\'' + ", sifra='" + sifra + '\'' +
", telefon='" + telefon + '\'' + ", telefon='" + telefon + '\'' +
", tipId=" + tipId + ", tipId=" + tipId +
", slika='" + slika + '\'' +
'}'; '}';
} }
} }
package com.example.SkuciSe.model.korisnik;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.Collection;
public class KorisnikDetails implements UserDetails {
private Korisnik korisnik;
public Korisnik getKorisnik()
{
return korisnik;
}
public KorisnikDetails(Korisnik user) {
this.korisnik = korisnik;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
}
@Override
public String getPassword() {
return korisnik.getEmail();
}
@Override
public String getUsername() {
return korisnik.getEmail();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
}
package com.example.SkuciSe.model.korisnik;
import com.example.SkuciSe.repository.KorisnikRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
public class KorisnikDetailsService implements UserDetailsService
{
@Autowired
KorisnikRepository kRepo;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Korisnik korisnik = kRepo.findByEmail(email);
if (korisnik != null)
{
return new KorisnikDetails( korisnik);
}
return null;
}
}
package com.example.SkuciSe.model; package com.example.SkuciSe.model.oglas;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
......
package com.example.SkuciSe.repository; package com.example.SkuciSe.repository;
import com.example.SkuciSe.model.korisnik.Korisnik;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.sql.Connection; import java.sql.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
@Component @Component
public class KorisnikRepository public class KorisnikRepository
{ {
Connection connection = null; Connection connection = null;
Statement statement = null; Statement statement = null;
public KorisnikRepository() public KorisnikRepository()
{ {
try { try {
...@@ -22,4 +19,30 @@ public class KorisnikRepository ...@@ -22,4 +19,30 @@ public class KorisnikRepository
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public void insert(Korisnik korisnik)
{
String sql = "insert into korisnik( ime, prezime, telefon, email, sifra, tipId) values('"+korisnik.getIme()+"','"+korisnik.getPrezime()+"','"+korisnik.getTelefon()+"','"+korisnik.getEmail()+"','"+korisnik.getSifra()+"',1)";
try {
statement.executeUpdate( sql);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
public Korisnik findByEmail(String email)
{
String sql = "select * from korisnik where email = '"+email+"'";
ResultSet rs = null;
try {
rs = statement.executeQuery( sql);
while( rs.next())
{
return ( new Korisnik( rs.getInt("korisnikId"), rs.getString("ime"), rs.getString("prezime"), rs.getString("email"), rs.getString("sifra"), rs.getString("telefon"), rs.getInt("tipid")));
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
return( null);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment