Skip to content
Snippets Groups Projects
Commit fc832869 authored by Mehdi Mohamed Mahmoud's avatar Mehdi Mohamed Mahmoud
Browse files

Merge branch 'master' into 'main'

Push existing project to GitLab

See merge request !1
parents 557dfdae 52ca541b
No related branches found
No related tags found
1 merge request!1Push existing project to GitLab
package org.local.security;
import jakarta.transaction.Transactional;
import org.local.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
@Configuration
public class WebSecurityConfig {
@Autowired
UserDao userDao;
@Bean
public DaoAuthenticationProvider authenticationProvider() {
DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
authProvider.setUserDetailsService(userDetailsService());
authProvider.setPasswordEncoder(passwordEncoder());
return authProvider;
}
@Bean
public UserDetailsService userDetailsService() {
return username -> UserDetailsImpl.build(userDao.findByName(username).get());
}
@Bean
public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
return authConfig.getAuthenticationManager();
}
@Bean
public AuthTokenFilter authenticationJwtTokenFilter() {
return new AuthTokenFilter();
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http.csrf(AbstractHttpConfigurer::disable)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
.authorizeHttpRequests(auth ->
auth.requestMatchers("/users/**",
"/swagger-ui/**",
"/v3/api-docs/**")
.permitAll()
.anyRequest().authenticated()
)
.authenticationProvider(authenticationProvider())
.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);
return http.build();
}
}
package org.local.service;
import com.fathzer.soft.javaluator.DoubleEvaluator;
import org.local.dao.ExpressionDao;
import org.local.dao.UserDao;
import org.local.model.Expression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
@Service
public class CalculatorService {
@Autowired
ExpressionDao expressionDao;
@Autowired
UserDao userDao;
Logger logger = LoggerFactory.getLogger(CalculatorService.class);
public Expression CalculateExpression(Expression expression,String UserId){
DoubleEvaluator evaluator = new DoubleEvaluator();
String result = "nan";
try {
result = String.valueOf(evaluator.evaluate(expression.getExpression()));
} catch (IllegalArgumentException e){
logger.error(e.getMessage());
}
if(result.equals("Infinity")) result = "nan";
expression.setAnswer(result);
if(!result.equals("nan")){
expression.setUser(userDao.findById(UserId).get());
expressionDao.save(expression);
}
return expression;
}
public Page<Expression> getExpressionsFromUser(String id,int pageNumber){
return expressionDao.findByUser(userDao.findById(id).get(), PageRequest.of(pageNumber,10));
}
}
package org.local.service;
import org.local.dao.UserDao;
import org.local.model.User;
import org.local.security.UserDetailsImpl;
import org.local.security.TokenService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
UserDao userDao;
@Autowired
AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService userDetailsService;
@Autowired
PasswordEncoder encoder;
@Autowired
TokenService tokenService;
Logger logger = LoggerFactory.getLogger(UserService.class);
public String checkUser(String name, String password){
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(name, password));
UserDetailsImpl userDetails = (UserDetailsImpl) authentication.getPrincipal();
return tokenService.generateToken(userDetails.getId());
}
public String createUser(String name, String password){
User user = new User();
user.setName(name);
user.setPassword(encoder.encode(password));
User userResult = userDao.save(user);
return tokenService.generateToken(userResult.getId());
}
}
keyStr = testsecrettestsecrettestsecrettestsecrettestsecret
logging.level.org.springframework.web: TRACE
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto= update
spring.h2.console.enabled=true
# default path: h2-console
spring.h2.console.path=/h2-ui
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment