This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JWTAuthorizationFilter extends BasicAuthenticationFilter { | |
public JWTAuthorizationFilter(AuthenticationManager authManager) { | |
super(authManager); | |
} | |
@Override | |
protected void doFilterInternal(HttpServletRequest req, | |
HttpServletResponse res, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController | |
@RequestMapping("/developers") | |
public class DeveloperController { | |
private DeveloperRepository developerRepository; | |
private BCryptPasswordEncoder bCryptPasswordEncoder; | |
public DeveloperController(DeveloperRepository developerRepository, | |
BCryptPasswordEncoder bCryptPasswordEncoder) { | |
this.developerRepository = developerRepository; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface DeveloperRepository extends JpaRepository<Developer, Long> { | |
Developer findByUsername(String username); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class Developer { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private long id; | |
private String username; | |
private String password; | |
public long getId() { | |
return id; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JWTAuthenticationFilter extends GenericFilterBean { | |
@Override | |
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, | |
FilterChain filterChain) throws IOException, ServletException { | |
Authentication auth = TokenAuthenticationService.getAuth((HttpServletRequest) servletRequest); | |
SecurityContextHolder.getContext().setAuthentication(auth); | |
filterChain.doFilter(servletRequest, servletResponse); | |
} | |
static Authentication getAuthentication(HttpServletRequest request) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class JWTLoginFilter extends AbstractAuthenticationProcessingFilter { | |
public JWTLoginFilter(String defaultFilterProcessesUrl, AuthenticationManager authManager) { | |
super(defaultFilterProcessesUrl); | |
setAuthenticationManager(authManager); | |
} | |
@Override | |
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response) throws AuthenticationException, IOException, ServletException { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableWebSecurity | |
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
http.csrf().disable().authorizeRequests() | |
.antMatchers("/").permitAll() | |
.antMatchers(HttpMethod.POST, "/login").permitAll() | |
.anyRequest().authenticated() | |
.and() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TokenAuthenticationService { | |
static final long EXPIRATIONTIME = 216_000_000; // 2.5 gün | |
static final String SECRET = "Emakina"; | |
static final String TOKEN_PREFIX = "Bearer "; | |
static final String HEADER_STRING = "Authorization"; | |
//Authenticate olmus user'a JWT yollamak icin | |
static void addAuth(HttpServletResponse response, String username) { | |
String JWT = Jwts.builder() |
NewerOlder