Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion homework-g596-ivanova/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mipt-java-2016</artifactId>
<groupId>ru.mipt.java2016</groupId>
<groupId>
ru.mipt.java2016</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>homework-g596-ivanova</artifactId>

<properties>
<spring.boot.version>1.4.2.RELEASE</spring.boot.version>
</properties>
<dependencies>
<dependency>
<groupId>ru.mipt.java2016</groupId>
Expand All @@ -35,8 +40,66 @@
<version>3.21.0-GA</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>net.sourceforge.jeval</groupId>
<artifactId>jeval</artifactId>
<version>0.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring.boot.version}</version>
</dependency>

<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.5.1</version>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>
</dependencies>

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>

<build>
<plugins>
<!-- Prepares Agent JAR before test execution -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ru.mipt.java2016.homework.g596.ivanova.task4;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class BillingDao {
private static final Logger LOG = LoggerFactory.getLogger(BillingDao.class);

@Autowired
private DataSource dataSource;

private JdbcTemplate jdbcTemplate;

@PostConstruct
public void postConstruct() {
jdbcTemplate = new JdbcTemplate(dataSource, false);
initSchema();
}

public void initSchema() {
LOG.trace("Initializing schema");
jdbcTemplate.execute("CREATE SCHEMA IF NOT EXISTS billing");
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.users " +
"(username VARCHAR PRIMARY KEY, password VARCHAR, enabled BOOLEAN)");
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.variables " +
"(username VARCHAR, name VARCHAR, value DOUBLE)");
jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.functions " +
"(username VARCHAR, name VARCHAR, arguments VARCHAR, expression VARCHAR)");
addUserIfNotExists("username", "password", true);
}

boolean addUserIfNotExists(String username, String password, boolean enabled) {
try {
loadUser(username);
return false;
} catch (EmptyResultDataAccessException e) {
jdbcTemplate.update("INSERT INTO billing.users VALUES (?, ?, ?)",
new Object[]{username, password, enabled});
return true;
}
}

public BillingUser loadUser(String username) throws EmptyResultDataAccessException {
LOG.trace("Querying for user " + username);
return jdbcTemplate.queryForObject(
"SELECT username, password, enabled FROM billing.users WHERE username = ?",
new Object[]{username},
new RowMapper<BillingUser>() {
@Override
public BillingUser mapRow(ResultSet rs, int rowNum) throws SQLException {
return new BillingUser(
rs.getString("username"),
rs.getString("password"),
rs.getBoolean("enabled")
);
}
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.mipt.java2016.homework.g596.ivanova.task4;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class BillingDatabaseConfiguration {
@Bean
public DataSource billingDataSource(
@Value("${ru.mipt.java2016.homework.g596.ivanova.task4.jdbcUrl}") String jdbcUrl,
@Value("${ru.mipt.java2016.homework.g596.ivanova.task4.username:}") String username,
@Value("${ru.mipt.java2016.homework.g596.ivanova.task4.password:}") String password
) {
HikariConfig config = new HikariConfig();
config.setDriverClassName(org.h2.Driver.class.getName());
config.setJdbcUrl(jdbcUrl);
config.setUsername(username);
config.setPassword(password);
return new HikariDataSource(config);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package ru.mipt.java2016.homework.g596.ivanova.task4;

public class BillingUser {
private final String username;
private final String password;
private final boolean enabled;

public BillingUser(String username, String password, boolean enabled) {
if (username == null) {
throw new IllegalArgumentException("Null username is not allowed");
}
if (password == null) {
throw new IllegalArgumentException("Null password is not allowed");
}
this.username = username;
this.password = password;
this.enabled = enabled;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public boolean isEnabled() {
return enabled;
}

@Override
public String toString() {
return "BillingUser{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", enabled=" + enabled +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

BillingUser that = (BillingUser) o;

if (enabled != that.enabled) {
return false;
}
if (!username.equals(that.username)) {
return false;
}
return password.equals(that.password);

}

@Override
public int hashCode() {
int result = username.hashCode();
result = 31 * result + password.hashCode();
result = 31 * result + (enabled ? 1 : 0);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.mipt.java2016.homework.g596.ivanova.task4;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import ru.mipt.java2016.homework.g596.ivanova.task1.BestCalculatorEver;

@EnableAutoConfiguration
@Configuration
@ComponentScan(basePackageClasses = RestCalculatorApplication.class)
public class RestCalculatorApplication {
@Bean
public BestCalculatorEver calculator() {
return new BestCalculatorEver();
}

@Bean
public EmbeddedServletContainerCustomizer customizer(
@Value("${ru.mipt.java2016.homework.g596.ivanova.task4.httpPort:9001}") int port) {
return container -> container.setPort(port);
}

public static void main(String[] args) {
SpringApplication application = new SpringApplication(RestCalculatorApplication.class);
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.mipt.java2016.homework.g596.ivanova.task4;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import ru.mipt.java2016.homework.base.task1.ParsingException;
import ru.mipt.java2016.homework.g596.ivanova.task1.BestCalculatorEver;

@RestController
public class RestCalculatorController {
@Autowired
private BestCalculatorEver calculator;

@Autowired
private BillingDao billingDao;

@RequestMapping(path = "/calculate", method = RequestMethod.POST,
consumes = "text/plain", produces = "text/plain")
public String calculate(Authentication authentication, @RequestBody String expression) throws
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А где все те операции из задания с добавлением/удалением переменных и функций?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Их пока что нет :(

ParsingException {
return calculator.calculate(expression) + "\n";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package ru.mipt.java2016.homework.g596.ivanova.task4;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.Collections;

@Configuration
@EnableWebSecurity
public class SecurityServiceConfiguration extends WebSecurityConfigurerAdapter {
private static final Logger LOG = LoggerFactory.getLogger(SecurityServiceConfiguration.class);

@Autowired
private BillingDao billingDao;

@Override
protected void configure(HttpSecurity http) throws Exception {
LOG.info("Configuring security");
http
.httpBasic().realmName("Calculator").and()
.formLogin().disable()
.logout().disable()
.csrf().disable()
.authorizeRequests()
.antMatchers("/calculate/**").authenticated()
.anyRequest().permitAll();
}

@Autowired
public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception {
LOG.info("Registering global user details service");
auth.userDetailsService(username -> {
try {
BillingUser user = billingDao.loadUser(username);
return new User(
user.getUsername(),
user.getPassword(),
Collections.singletonList(() -> "AUTH")
);
} catch (EmptyResultDataAccessException e) {
LOG.warn("No such user: " + username);
throw new UsernameNotFoundException(username);
}
});
}
}