Skip to content

Instantly share code, notes, and snippets.

View ugurcemozturk's full-sized avatar
:shipit:
Stalking your pins

Ugur Cem Ozturk ugurcemozturk

:shipit:
Stalking your pins
View GitHub Profile
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()
@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()
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 {
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) {
@Entity
public class Developer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String username;
private String password;
public long getId() {
return id;
public interface DeveloperRepository extends JpaRepository<Developer, Long> {
Developer findByUsername(String username);
}
@RestController
@RequestMapping("/developers")
public class DeveloperController {
private DeveloperRepository developerRepository;
private BCryptPasswordEncoder bCryptPasswordEncoder;
public DeveloperController(DeveloperRepository developerRepository,
BCryptPasswordEncoder bCryptPasswordEncoder) {
this.developerRepository = developerRepository;
public class JWTAuthorizationFilter extends BasicAuthenticationFilter {
public JWTAuthorizationFilter(AuthenticationManager authManager) {
super(authManager);
}
@Override
protected void doFilterInternal(HttpServletRequest req,
HttpServletResponse res,
public class SecurityConstants {
public static final String SECRET = "Emakina";
public static final long EXPIRATION_TIME = 423_000_000; // 5 gün
public static final String TOKEN_PREFIX = "Bearer ";
public static final String HEADER_STRING = "Authorization";
public static final String SIGN_UP_URL = "/developers/sign-up";
}
public class JWTAuthenticationFilter extends UsernamePasswordAuthenticationFilter {
private AuthenticationManager authenticationManager;
public JWTAuthenticationFilter(AuthenticationManager authenticationManager) {
this.authenticationManager = authenticationManager;
}
@Override