[ADD] CHANGES

parent 16329228
...@@ -21,15 +21,33 @@ ...@@ -21,15 +21,33 @@
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId> <artifactId>spring-boot-starter-security</artifactId>
<version>3.2.0</version> <version>3.2.0</version>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<!-- JPA --> <!-- JPA -->
<dependency> <dependency>
......
...@@ -2,7 +2,6 @@ package web.multitask.app.api; ...@@ -2,7 +2,6 @@ package web.multitask.app.api;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import web.multitask.app.mysql.ProcedureMysql; import web.multitask.app.mysql.ProcedureMysql;
import web.multitask.app.repository.UserRespository; import web.multitask.app.repository.UserRespository;
......
...@@ -34,8 +34,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -34,8 +34,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
} }
@Bean @Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration) throws Exception { public AuthenticationManager authenticationManager(AuthenticationConfiguration authenticationConfiguration)
return authenticationConfiguration.getAuthenticationManager(); throws Exception {
return authenticationConfiguration.getAuthenticationManager();
} }
@Override @Override
...@@ -45,17 +46,18 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -45,17 +46,18 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override @Override
protected void configure(HttpSecurity http) throws Exception { protected void configure(HttpSecurity http) throws Exception {
http http.cors(AbstractHttpConfigurer::disable).csrf(AbstractHttpConfigurer::disable)
.cors(AbstractHttpConfigurer::disable).csrf(AbstractHttpConfigurer::disable)
.authorizeHttpRequests( .authorizeHttpRequests(
authorizeRequests -> authorizeRequests authorizeRequests -> authorizeRequests
.antMatchers("/test/admin").hasAuthority("ADMIN") .antMatchers("/security/**").hasAnyAuthority("ADMIN")
.antMatchers("/test/user").hasAuthority("USER") .antMatchers("/api/**").hasAnyAuthority("ADMIN", "USER")
.antMatchers(HttpMethod.GET, "/**").permitAll() .antMatchers("/token/**").permitAll()
.antMatchers(HttpMethod.POST, "/**").permitAll() .antMatchers("/private/**").hasAnyAuthority("ADMIN","USER")
.antMatchers("/public/**").permitAll()
.antMatchers(HttpMethod.GET, "/**").permitAll()
.anyRequest() .anyRequest()
.authenticated()); .authenticated());
http.addFilterBefore(new JwtTokenFilter(jwtTokenUtil,userRepo), UsernamePasswordAuthenticationFilter.class); http.addFilterBefore(new JwtTokenFilter(jwtTokenUtil, userRepo), UsernamePasswordAuthenticationFilter.class);
} }
@Bean @Bean
...@@ -63,10 +65,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { ...@@ -63,10 +65,9 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
return new BCryptPasswordEncoder(); return new BCryptPasswordEncoder();
} }
@Bean @Bean
public CorsFilter corsFilter() { public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration(); CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true); config.setAllowCredentials(true);
config.addAllowedOrigin("*"); config.addAllowedOrigin("*");
......
...@@ -36,32 +36,31 @@ public class JwtTokenFilter extends OncePerRequestFilter { ...@@ -36,32 +36,31 @@ public class JwtTokenFilter extends OncePerRequestFilter {
} }
@Override @Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException, java.io.IOException { protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException, java.io.IOException {
final String header = request.getHeader(HttpHeaders.AUTHORIZATION); final String header = request.getHeader(HttpHeaders.AUTHORIZATION);
if (request.getRequestURI().startsWith("/token")) { String token = "";
try{
token = header.split(" ")[1];
}catch (Exception e){
token = null;
}
if (token == null || token.isEmpty()) {
chain.doFilter(request, response); chain.doFilter(request, response);
} else { } else {
if (header == null || !header.startsWith("Bearer ")) { if (jwtTokenUtil.validateToken(token)) {
response.sendError(403, "Access Denied"); JSONObject jsonToken = new JSONObject(jwtTokenUtil.getDataToken(token));
UserDetails userDetails = userRepo.findByUsername(jsonToken.getString("username"));
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
userDetails,
null, userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authentication);
chain.doFilter(request, response); chain.doFilter(request, response);
} else { } else {
String token = header.split(" ")[1]; response.sendError(401, "Invalid Token");
if (jwtTokenUtil.validateToken(token)) {
JSONObject jsonToken = new JSONObject(jwtTokenUtil.getDataToken(token));
UserDetails userDetails = userRepo.findByUsername(jsonToken.getString("username"));
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
authentication.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authentication);
response.setStatus(200);
chain.doFilter(request, response);
} else {
response.sendError(401, "Invalid Token");
chain.doFilter(request, response);
}
} }
} }
} }
} }
\ No newline at end of file
...@@ -42,6 +42,8 @@ public class ProcedureMysql { ...@@ -42,6 +42,8 @@ public class ProcedureMysql {
List<Map<String, Object>> list = jdbcTemplate.queryForList(query.toString(), params); List<Map<String, Object>> list = jdbcTemplate.queryForList(query.toString(), params);
JSONObject result = new JSONObject(); JSONObject result = new JSONObject();
result.put("data", list); result.put("data", list);
result.put("message", "Success");
result.put("status", true);
return result; return result;
} catch (Exception e) { } catch (Exception e) {
return new JSONObject().put("data", new JSONObject()).put("message", e.getMessage()).put("status", false); return new JSONObject().put("data", new JSONObject()).put("message", e.getMessage()).put("status", false);
......
...@@ -24,7 +24,7 @@ public class JwtTokenUtil implements Serializable{ ...@@ -24,7 +24,7 @@ public class JwtTokenUtil implements Serializable{
JSONObject json = new JSONObject(); JSONObject json = new JSONObject();
json.put("username", user.getUsername()); json.put("username", user.getUsername());
return Jwts.builder() return Jwts.builder()
.setSubject(json.toString()) .setSubject(json.toString())
.setIssuedAt(new Date()) .setIssuedAt(new Date())
.setExpiration(expiryDate) .setExpiration(expiryDate)
.signWith(Keys.hmacShaKeyFor(jwtSecret.getBytes())) .signWith(Keys.hmacShaKeyFor(jwtSecret.getBytes()))
......
...@@ -4,7 +4,7 @@ spring.datasource.password=asd123 ...@@ -4,7 +4,7 @@ spring.datasource.password=asd123
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
server.port=8081 server.port=8081
server.address=0.0.0.0 server.address=0.0.0.0
spring.jpa.show-sql=true # spring.jpa.show-sql=true
app.jwtSecret=9a4f2c8d3b7a1e6f45c8a0b3f267d8b1d4e6f3c8a9d2b5f8e3a9c8b5f6v8a3d9 app.jwtSecret=9a4f2c8d3b7a1e6f45c8a0b3f267d8b1d4e6f3c8a9d2b5f8e3a9c8b5f6v8a3d9
spring.jpa.hibernate.ddl-auto = update spring.jpa.hibernate.ddl-auto = update
spring.security.filter.order=1 spring.security.filter.order=1
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment