Skip to content

Commit

Permalink
5Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Marija26 committed Sep 25, 2018
1 parent 71b1d4f commit db0a323
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 97 deletions.
30 changes: 27 additions & 3 deletions Presentation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,31 @@
<artifactId>spring-security-taglibs</artifactId>
<version>5.0.3.RELEASE</version>
</dependency>
</dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<version>2.0.4.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.8.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
<!--<dependency>-->
<!--<groupId>javax.servlet</groupId>-->
<!--<artifactId>javax.servlet-api</artifactId>-->
<!--<version>4.0.1</version>-->
<!--<scope>test</scope>-->
<!--</dependency>-->
</dependencies>
<repositories>
<!-- ... possibly other repository elements ... -->
<repository>
Expand All @@ -56,8 +80,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>6</source>
<target>6</target>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
Expand Down
13 changes: 12 additions & 1 deletion Presentation/src/main/java/org/mary/pro/DAO/UserDAO.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
package org.mary.pro.DAO;

import org.mary.pro.model.User;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import java.util.*;

@Component
@Repository
public class UserDAO {



public UserDAO() {

}

private static final Map<String, User> UMap = new HashMap<String, User>();

static {
Expand Down Expand Up @@ -47,4 +55,7 @@ public List<User> getAllUsers() {
list.addAll(c);
return list;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

// excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = Configuration.class) })
@Configuration
@EnableWebMvc
@Import(SecurityConfig.class)
@ComponentScan("org.mary.pro.*")
@ComponentScan(basePackages = {"org.mary.pro.*"})

public class ApplicationContextConfig{
public ApplicationContextConfig(){
super();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.IOException;
import java.io.PrintWriter;


@Component
public class MyBasicAuthenticationEntryPoint extends BasicAuthenticationEntryPoint {

Expand Down
77 changes: 19 additions & 58 deletions Presentation/src/main/java/org/mary/pro/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,35 @@
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.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configurers.provisioning.InMemoryUserDetailsManagerConfigurer;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
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.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.FilterChainProxy;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@EnableWebMvc
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

public static final String REALM_NAME = "org.mary.pro";

@Autowired
private MyBasicAuthenticationEntryPoint authenticationEntryPoint;


@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();

// All requests send to the Web Server request must be authenticated
http.authorizeRequests().anyRequest().authenticated();

// Use AuthenticationEntryPoint to authenticate user/password
http.httpBasic().authenticationEntryPoint(authenticationEntryPoint);


Expand All @@ -46,66 +43,30 @@ public BCryptPasswordEncoder passwordEncoder() {
return bCryptPasswordEncoder;
}

@Bean
public UserDetailsService userDetailsServiceBean() throws Exception {
return super.userDetailsServiceBean();
}
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {

return super.authenticationManagerBean();
}


@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
String password = "123";
String encrytedPassword = this.passwordEncoder().encode(password);

InMemoryUserDetailsManagerConfigurer<AuthenticationManagerBuilder> //
mngConfig = auth.inMemoryAuthentication();

mngConfig = auth.inMemoryAuthentication();

UserDetails u1 = User.withUsername("tom").password(encrytedPassword).roles("USER").build();
UserDetails u2 = User.withUsername("jerry").password(encrytedPassword).roles("USER").build();
UserDetails u1 = User.withUsername("manager").password(encrytedPassword).roles("MANAGER").build();
UserDetails u2 = User.withUsername("user").password(encrytedPassword).roles("USER").build();

mngConfig.withUser(u1);
mngConfig.withUser(u2);
// System.out.println("Encoded password of 123=" + encrytedPassword);
// auth.inMemoryAuthentication()
// .withUser("user").password("password")
// .authorities("ROLE_USER");
// }

// @Override
// public void configure(WebSecurity webSecurity) throws Exception {
// webSecurity
// .ignoring()
// .antMatchers("/resources/**");}

// @Override
// protected void configure(HttpSecurity http) throws Exception {

// .authorizeRequests()
// .antMatchers("/signup","/about").permitAll() // #4
// .antMatchers("/admin/**").hasRole("ADMIN") // #6
// .anyRequest().authenticated() // 7
// .and()
// .formLogin() // #8
//// .loginUrl("/login") // #9
// .permitAll(); // #5

// http.authorizeRequests()
// .antMatchers("/admin/**").access("hasRole('ROLE_ADMIN')")
// .antMatchers("/dba/**").access("hasRole('ROLE_ADMIN') or hasRole('ROLE_DBA')")
// .and().formLogin();

}
}


// http.authorizeRequests()
// .antMatchers("/securityNone").permitAll()
// .anyRequest().authenticated()
// .and()
// .httpBasic()
// .authenticationEntryPoint(authenticationEntryPoint);
//
// http.addFilterAfter(new CustomFilter(),
// BasicAuthenticationFilter.class);
// }
//
// @Bean
// public PasswordEncoder passwordEncoder() {
// return new BCryptPasswordEncoder();
// }
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.mary.pro.config;

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
//
// public SpringSecurityInitializer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;


public class SpringWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {



@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext appContext = new AnnotationConfigWebApplicationContext();
appContext.register(ApplicationContextConfig.class, SecurityConfig.class);
appContext.register(ApplicationContextConfig.class);
// SecurityConfig.class);

servletContext.addListener(new ContextLoaderListener(appContext));

Expand Down Expand Up @@ -46,9 +48,11 @@ public void onStartup(ServletContext servletContext) throws ServletException {
encodingFilter.addMappingForUrlPatterns(null, true, "/*");
}
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SecurityConfig.class };
protected Class<?>[] getRootConfigClasses() {
return new Class[]{};
}
// SecurityConfig.class };
// }

@Override
protected Class<?>[] getServletConfigClasses() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController

public class UserController {

@Autowired

public UserController() {
}

@Autowired
private UserDAO dao;

@RequestMapping("/")
Expand All @@ -23,6 +27,7 @@ public String welcome() {
}



@RequestMapping(value = "/users",
method = RequestMethod.GET,
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
Expand All @@ -35,9 +40,11 @@ public List<User> getUsers() {

@RequestMapping(value = "/user/{lastName}",
method = RequestMethod.GET, //
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE })
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@ResponseBody
public User getUser(@PathVariable("lastName") String lastName) {
public User getUser(@PathVariable("lastName") String lastName)throws IOException

{
return dao.getUser(lastName);
}

Expand Down
48 changes: 24 additions & 24 deletions Presentation/src/main/java/org/mary/pro/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,28 @@ public void setPosition(String position) {
this.position = position;
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(name, user.name) &&
Objects.equals(lastName, user.lastName) &&
Objects.equals(position, user.position);
}

@Override
public int hashCode() {
return Objects.hash(name, lastName, position);
}

@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", lastName='" + lastName + '\'' +
", position='" + position + '\'' +
'}';
}
//
// @Override
// public boolean equals(Object o) {
// if (this == o) return true;
// if (o == null || getClass() != o.getClass()) return false;
// User user = (User) o;
// return Objects.equals(name, user.name) &&
// Objects.equals(lastName, user.lastName) &&
// Objects.equals(position, user.position);
// }
//
// @Override
// public int hashCode() {
// return Objects.hash(name, lastName, position);
// }
//
// @Override
// public String toString() {
// return "User{" +
// "name='" + name + '\'' +
// ", lastName='" + lastName + '\'' +
// ", position='" + position + '\'' +
// '}';
// }
}
9 changes: 9 additions & 0 deletions Presentation/src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=INFO, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
Loading

0 comments on commit db0a323

Please sign in to comment.