Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
E
Ecsite
Project
Project
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
shoei.kanno
Ecsite
Commits
9d431c28
Commit
9d431c28
authored
Oct 14, 2020
by
keita.onoguchi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ログイン機能実装途中
parent
6ca8e1dd
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
173 additions
and
0 deletions
+173
-0
SecurityConfig.java
src/main/java/com/example/SecurityConfig.java
+47
-0
LoginUser.java
src/main/java/com/example/domain/LoginUser.java
+39
-0
User.java
src/main/java/com/example/domain/User.java
+48
-0
UserRepository.java
src/main/java/com/example/repository/UserRepository.java
+11
-0
LoginUserDetailsService.java
...ain/java/com/example/service/LoginUserDetailsService.java
+28
-0
No files found.
src/main/java/com/example/SecurityConfig.java
0 → 100644
View file @
9d431c28
package
com
.
example
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.security.config.annotation.web.builders.HttpSecurity
;
import
org.springframework.security.config.annotation.web.builders.WebSecurity
;
import
org.springframework.security.config.annotation.web.configuration.EnableWebSecurity
;
import
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.security.crypto.password.Pbkdf2PasswordEncoder
;
@EnableWebSecurity
public
class
SecurityConfig
extends
WebSecurityConfigurerAdapter
{
@Bean
PasswordEncoder
passwordEncoder
(){
return
new
Pbkdf2PasswordEncoder
();
}
@Override
public
void
configure
(
WebSecurity
web
)
throws
Exception
{
web
.
debug
(
false
)
.
ignoring
()
.
antMatchers
(
"/images/**"
,
"/js/**"
,
"/css/**"
)
;
}
@Override
protected
void
configure
(
HttpSecurity
http
)
throws
Exception
{
http
.
authorizeRequests
()
.
mvcMatchers
(
"/"
,
"/signup"
,
"/login"
).
permitAll
()
.
mvcMatchers
(
"/limited/admin/**"
).
hasRole
(
"ADMIN"
)
.
anyRequest
().
authenticated
()
.
and
()
.
formLogin
()
.
defaultSuccessUrl
(
"/top"
)
.
and
()
.
logout
()
.
invalidateHttpSession
(
true
)
.
deleteCookies
(
"JSESSIONID"
)
.
logoutSuccessUrl
(
"/login"
);
}
}
src/main/java/com/example/domain/LoginUser.java
0 → 100644
View file @
9d431c28
package
com
.
example
.
domain
;
import
java.util.Collections
;
import
java.util.Set
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
import
org.springframework.security.core.GrantedAuthority
;
import
org.springframework.security.core.authority.SimpleGrantedAuthority
;
import
lombok.extern.slf4j.Slf4j
;
//ユーザーの認証・認可にかかわるクラス
@Slf4j
public
class
LoginUser
extends
org
.
springframework
.
security
.
core
.
userdetails
.
User
{
private
static
final
long
serialVersionUID
=
1L
;
private
User
user
;
public
LoginUser
(
User
user
){
super
(
user
.
getEmail
(),
user
.
getPassword
(),
convertGrantedAuthorities
(
user
.
getRoles
()));
this
.
user
=
user
;
}
public
User
getUser
()
{
return
user
;
}
static
Set
<
GrantedAuthority
>
convertGrantedAuthorities
(
String
roles
){
if
(
roles
==
null
||
roles
.
isEmpty
()){
return
Collections
.
emptySet
();
}
Set
<
GrantedAuthority
>
authorities
=
Stream
.
of
(
roles
.
split
(
","
))
.
map
(
SimpleGrantedAuthority:
:
new
)
.
collect
(
Collectors
.
toSet
());
return
authorities
;
}
}
src/main/java/com/example/domain/User.java
0 → 100644
View file @
9d431c28
package
com
.
example
.
domain
;
import
java.sql.Date
;
import
java.util.Collection
;
import
javax.persistence.Column
;
import
javax.persistence.Entity
;
import
javax.persistence.GeneratedValue
;
import
javax.persistence.Id
;
import
javax.persistence.Table
;
import
org.springframework.security.core.GrantedAuthority
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.ToString
;
//ログイン用のエンティティークラス
@Data
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table
(
name
=
"users"
)
@ToString
(
exclude
=
{
"email"
,
"password"
})
public
class
User
{
@GeneratedValue
private
int
id
;
@Column
(
nullable
=
false
)
private
String
name
;
@Column
(
nullable
=
false
)
private
String
postalCode
;
@Column
(
nullable
=
false
)
private
String
address
;
@Column
(
nullable
=
false
)
private
String
phoneNumber
;
@Id
@Column
(
nullable
=
false
)
private
String
email
;
@Column
(
nullable
=
false
)
private
String
password
;
@Column
(
nullable
=
false
)
private
String
roles
;
@Column
(
nullable
=
false
)
private
Date
createdAt
;
@Column
(
nullable
=
false
)
private
Date
updatedAt
;
}
src/main/java/com/example/repository/UserRepository.java
0 → 100644
View file @
9d431c28
package
com
.
example
.
repository
;
import
java.util.Optional
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
com.example.domain.User
;
public
interface
UserRepository
extends
JpaRepository
<
User
,
String
>{
Optional
<
User
>
findByEmail
(
String
email
);
}
src/main/java/com/example/service/LoginUserDetailsService.java
0 → 100644
View file @
9d431c28
package
com
.
example
.
service
;
import
org.springframework.security.core.userdetails.UserDetails
;
import
org.springframework.security.core.userdetails.UserDetailsService
;
import
org.springframework.security.core.userdetails.UsernameNotFoundException
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
com.example.repository.UserRepository
;
import
lombok.extern.slf4j.Slf4j
;
@Service
@Slf4j
public
class
LoginUserDetailsService
implements
UserDetailsService
{
private
final
UserRepository
userRepository
;
public
LoginUserDetailsService
(
UserRepository
userRepository
){
this
.
userRepository
=
userRepository
;
}
@Transactional
(
readOnly
=
true
)
@Override
public
UserDetails
loadUserByUsername
(
String
email
)
throws
UsernameNotFoundException
{
assert
(
email
!=
null
);
log
.
debug
();
}
}
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