Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit b8e3d42

Browse files
committed
Refreshtoken is ongoing...
1 parent 3c21dde commit b8e3d42

File tree

4 files changed

+89
-4
lines changed

4 files changed

+89
-4
lines changed

‎day-14/api/src/main/java/com/bookstore/api/config/ApplicationSecurityConfig.java‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@
33
import org.springframework.context.annotation.Bean;
44
import org.springframework.context.annotation.Configuration;
55
import org.springframework.http.HttpMethod;
6+
import org.springframework.security.authentication.AuthenticationManager;
67
import org.springframework.security.authentication.AuthenticationProvider;
78
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
9+
import org.springframework.security.config.BeanIds;
810
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
911
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
1012
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
1113
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1214
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
15+
import org.springframework.security.config.http.SessionCreationPolicy;
1316
import org.springframework.security.core.userdetails.User;
1417
import org.springframework.security.core.userdetails.UserDetails;
1518
import org.springframework.security.core.userdetails.UserDetailsService;
1619
import org.springframework.security.crypto.password.PasswordEncoder;
1720
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
1821

22+
import com.bookstore.api.jwt.JwtAuthenticationEntryPoint;
23+
import com.bookstore.api.jwt.JwtAuthenticationFilter;
1924
import com.bookstore.api.services.ApplicationUserService;
2025

2126
import lombok.RequiredArgsConstructor;
@@ -31,18 +36,33 @@ public class ApplicationSecurityConfig
3136

3237
private final PasswordEncoder passwordEncoder;
3338
private final ApplicationUserService applicationUserService;
39+
private final JwtAuthenticationEntryPoint handler;
40+
41+
42+
@Bean
43+
public JwtAuthenticationFilter jwtAuthenticationFilter(){
44+
return new JwtAuthenticationFilter();
45+
}
46+
47+
@Bean(BeanIds.AUTHENTICATION_MANAGER)
48+
@Override
49+
public AuthenticationManager authenticationManagerBean() throws Exception {
50+
return super.authenticationManagerBean();
51+
}
3452

3553
@Override
3654
protected void configure(HttpSecurity http) throws Exception {
3755
http
3856
.csrf().disable()
57+
.exceptionHandling().authenticationEntryPoint(handler)
58+
.and()
59+
.sessionManagement()
60+
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
61+
.and()
3962
.authorizeRequests()
4063
.antMatchers("/api/v1/**").hasRole(ADMIN.name())
41-
.antMatchers("/api/v1/**").permitAll()
4264
.anyRequest()
43-
.authenticated()
44-
.and()
45-
.httpBasic();
65+
.authenticated();
4666
}
4767

4868
@Override
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.bookstore.api.entities;
2+
3+
import java.util.Date;
4+
5+
import javax.persistence.Column;
6+
import javax.persistence.Entity;
7+
import javax.persistence.FetchType;
8+
import javax.persistence.GeneratedValue;
9+
import javax.persistence.GenerationType;
10+
import javax.persistence.Id;
11+
import javax.persistence.JoinColumn;
12+
import javax.persistence.ManyToOne;
13+
import javax.persistence.Table;
14+
import javax.persistence.Temporal;
15+
import javax.persistence.TemporalType;
16+
17+
import org.hibernate.annotations.OnDelete;
18+
import org.hibernate.annotations.OnDeleteAction;
19+
20+
import com.fasterxml.jackson.annotation.JsonIgnore;
21+
22+
import lombok.AllArgsConstructor;
23+
import lombok.Data;
24+
import lombok.NoArgsConstructor;
25+
26+
@Entity
27+
@Table(name = "refresh_token")
28+
@Data
29+
@NoArgsConstructor
30+
@AllArgsConstructor
31+
public class RefreshToken {
32+
@Id
33+
@Column(name = "id")
34+
@GeneratedValue(strategy = GenerationType.IDENTITY)
35+
private int id;
36+
37+
@ManyToOne(fetch = FetchType.LAZY)
38+
@JoinColumn(name = "user_id", nullable = false)
39+
@OnDelete(action = OnDeleteAction.CASCADE)
40+
@JsonIgnore
41+
private User user;
42+
43+
@Column(name = "token", nullable = false, unique = true)
44+
private String token;
45+
46+
@Column(name = "expiry_date", nullable = false)
47+
@Temporal(TemporalType.TIMESTAMP)
48+
private Date expiryDate;
49+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.bookstore.api.repositories;
2+
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import com.bookstore.api.entities.RefreshToken;;
5+
6+
public interface RefreshTokenRepository extends JpaRepository<RefreshToken, Integer> {
7+
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.bookstore.api.services;
2+
3+
import org.springframework.stereotype.Service;
4+
5+
@Service
6+
public class RefreshTokenService {
7+
8+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /