Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
</properties>

<dependencies>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
Expand All @@ -33,7 +38,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

@SpringBootApplication
public class ServerApplication {

public static void main(String[] args) {
SpringApplication.run(ServerApplication.class, args);



}
}
63 changes: 46 additions & 17 deletions src/main/java/ru/amm/fileexplorer/server/config/SecurityConfig.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,66 @@
package ru.amm.fileexplorer.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.rememberme.JdbcTokenRepositoryImpl;
import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository;
import ru.amm.fileexplorer.server.service.UserDetailsServiceImpl;

import javax.sql.DataSource;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user")
.password(passwordEncoder()
.encode("test-password"))
.roles("ADMIN");

@Autowired
private UserDetailsServiceImpl userDetailsService;

@Autowired
private DataSource dataSource;

@Bean
public BCryptPasswordEncoder passwordEncoder() {
BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
return bCryptPasswordEncoder;
}

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {


auth.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());

}

@Override
protected void configure(HttpSecurity http) throws Exception {
http.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/css/**", "/scripts/**")
.permitAll()
.antMatchers("/**").not().anonymous();
}

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
http.authorizeRequests().antMatchers( "/login", "/logout").permitAll();

http.authorizeRequests().antMatchers("/").access("hasRole('ROLE_USER')");

http.authorizeRequests().and().exceptionHandling().accessDeniedPage("/403");

http.authorizeRequests().and().formLogin()
.loginProcessingUrl("/j_spring_security_check")
.loginPage("/login")
.defaultSuccessUrl("/")
.failureUrl("/login?error=true")
.usernameParameter("username")
.passwordParameter("password")
.and().logout().logoutUrl("/logout").logoutSuccessUrl("/login");



}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.amm.fileexplorer.server.config.entity;

import javax.persistence.*;

@Entity
@Table(name = "App_Role", //
uniqueConstraints = { //
@UniqueConstraint(name = "APP_ROLE_UK", columnNames = "Role_Name")})
public final class AppRole {

@Id
@GeneratedValue
@Column(name = "Role_Id", nullable = false)
private Long roleId;

@Column(name = "Role_Name", length = 30, nullable = false)
private String roleName;

public Long getRoleId() {
return roleId;
}

public void setRoleId(Long roleId) {
this.roleId = roleId;
}

public String getRoleName() {
return roleName;
}

public void setRoleName(String roleName) {
this.roleName = roleName;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ru.amm.fileexplorer.server.config.entity;

import javax.persistence.*;

@Entity
@Table(name = "App_User", //
uniqueConstraints = { //
@UniqueConstraint(name = "APP_USER_UK", columnNames = "User_Name")})
public class AppUser {

@Id
@GeneratedValue
@Column(name = "User_Id", nullable = false)
private Long userId;

@Column(name = "User_Name", length = 36, nullable = false)
private String userName;

@Column(name = "Encryted_Password", length = 128, nullable = false)
private String encrytedPassword;

@Column(name = "Enabled", length = 1, nullable = false)
private boolean enabled;

public Long getUserId() {
return userId;
}

public void setUserId(Long userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getEncrytedPassword() {
return encrytedPassword;
}

public void setEncrytedPassword(String encrytedPassword) {
this.encrytedPassword = encrytedPassword;
}

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ru.amm.fileexplorer.server.config.entity;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "User_Role", //
uniqueConstraints = { //
@UniqueConstraint(name = "USER_ROLE_UK", columnNames = { "User_Id", "Role_Id" }) })
public class UserRole {

@Id
@GeneratedValue
@Column(name = "Id", nullable = false)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "User_Id", nullable = false)
private AppUser appUser;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Role_Id", nullable = false)
private AppRole appRole;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public AppUser getAppUser() {
return appUser;
}

public void setAppUser(AppUser appUser) {
this.appUser = appUser;
}

public AppRole getAppRole() {
return appRole;
}

public void setAppRole(AppRole appRole) {
this.appRole = appRole;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
public class ErrorPageController implements ErrorController {
@RequestMapping(method = RequestMethod.GET)
public ModelAndView processError(HttpServletRequest req) {
int status = getErrorCode(req);
int status = getErrorCode( req );
String message = "Unknown error";
if (status == 404) {
if ( status == 404 ) {
message = "The requested page not found";
}
return new ModelAndView("error-page", "errorMessage", message);
return new ModelAndView( "error-page", "errorMessage", message );
}

private int getErrorCode(HttpServletRequest httpRequest) {
return (Integer) httpRequest.getAttribute("javax.servlet.error.status_code");
return (Integer) httpRequest.getAttribute( "javax.servlet.error.status_code" );
}

@Override
Expand Down
Loading