Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
SkuciSe
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Tim 2 - 2022
SkuciSe
Commits
9415f63c
Commit
9415f63c
authored
Aug 31, 2022
by
Bogdan Andjelkovic
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
register controller and config
parent
1d7914a5
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
205 additions
and
11 deletions
+205
-11
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSecurityConfig.java
+53
-0
SkuciSe/src/main/java/com/example/SkuciSe/controller/AppController.java
+44
-0
SkuciSe/src/main/java/com/example/SkuciSe/model/korisnik/Korisnik.java
+2
-5
SkuciSe/src/main/java/com/example/SkuciSe/model/korisnik/KorisnikDetails.java
+54
-0
SkuciSe/src/main/java/com/example/SkuciSe/model/korisnik/KorisnikDetailsService.java
+23
-0
SkuciSe/src/main/java/com/example/SkuciSe/model/oglas/Oglas.java
+1
-1
SkuciSe/src/main/java/com/example/SkuciSe/repository/KorisnikRepository.java
+28
-5
No files found.
SkuciSe/src/main/java/com/example/SkuciSe/configuration/WebSecurityConfig.java
0 → 100644
View file @
9415f63c
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
();
}
}
SkuciSe/src/main/java/com/example/SkuciSe/controller/AppController.java
0 → 100644
View file @
9415f63c
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"
);
}
}
SkuciSe/src/main/java/com/example/SkuciSe/model/Korisnik.java
→
SkuciSe/src/main/java/com/example/SkuciSe/model/
korisnik/
Korisnik.java
View file @
9415f63c
package
com
.
example
.
SkuciSe
.
model
;
package
com
.
example
.
SkuciSe
.
model
.
korisnik
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
...
...
@@ -18,16 +18,14 @@ public class Korisnik
String
sifra
;
String
telefon
;
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
.
prezime
=
prezime
;
this
.
email
=
email
;
this
.
sifra
=
sifra
;
this
.
telefon
=
telefon
;
this
.
tipId
=
tipId
;
this
.
slika
=
slika
;
}
@Override
...
...
@@ -40,7 +38,6 @@ public class Korisnik
", sifra='"
+
sifra
+
'\''
+
", telefon='"
+
telefon
+
'\''
+
", tipId="
+
tipId
+
", slika='"
+
slika
+
'\''
+
'}'
;
}
}
SkuciSe/src/main/java/com/example/SkuciSe/model/korisnik/KorisnikDetails.java
0 → 100644
View file @
9415f63c
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
;
}
}
SkuciSe/src/main/java/com/example/SkuciSe/model/korisnik/KorisnikDetailsService.java
0 → 100644
View file @
9415f63c
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
;
}
}
SkuciSe/src/main/java/com/example/SkuciSe/model/Oglas.java
→
SkuciSe/src/main/java/com/example/SkuciSe/model/
oglas/
Oglas.java
View file @
9415f63c
package
com
.
example
.
SkuciSe
.
model
;
package
com
.
example
.
SkuciSe
.
model
.
oglas
;
import
lombok.AllArgsConstructor
;
import
lombok.Getter
;
...
...
SkuciSe/src/main/java/com/example/SkuciSe/repository/KorisnikRepository.java
View file @
9415f63c
package
com
.
example
.
SkuciSe
.
repository
;
import
com.example.SkuciSe.model.korisnik.Korisnik
;
import
org.springframework.stereotype.Component
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.SQLException
;
import
java.sql.Statement
;
import
java.sql.*
;
@Component
public
class
KorisnikRepository
{
Connection
connection
=
null
;
Statement
statement
=
null
;
public
KorisnikRepository
()
{
try
{
...
...
@@ -22,4 +19,30 @@ public class KorisnikRepository
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
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment