emr_demo
@ 2
emr_demo / web_emr - Copy / src / main / java / com / bestray / config / WebSecurityConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
package com.bestray.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpMethod; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; 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.config.http.SessionCreationPolicy; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; import com.bestray.security.JwtAuthenticationEntryPoint; import com.bestray.security.JwtAuthorizationTokenFilter; import com.bestray.security.JwtTokenUtil; import com.bestray.security.service.JwtUserDetailsService; @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private JwtAuthenticationEntryPoint unauthorizedHandler; @Autowired private JwtTokenUtil jwtTokenUtil; @Autowired private JwtUserDetailsService jwtUserDetailsService; @Value("${jwt.header}") private String tokenHeader; @Value("${jwt.route.authentication.path}") private String authenticationPath; @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { auth .userDetailsService(jwtUserDetailsService) .passwordEncoder(passwordEncoderBean()); } @Bean public PasswordEncoder passwordEncoderBean() { return new BCryptPasswordEncoder(); } @Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); } @Override protected void configure(HttpSecurity httpSecurity) throws Exception{ httpSecurity // we don't need CSRF because our token is invulnerable .csrf().disable() .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and() // don't create session .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and() .authorizeRequests() // Un-secure H2 Database .antMatchers("/h2-console/**/**").permitAll() .antMatchers("/auth/**").permitAll() .antMatchers("/auth/registration").permitAll() .antMatchers("/result").permitAll() .antMatchers("/registration").permitAll() .antMatchers("/user/update/**").permitAll() .antMatchers("/user/delete/**").permitAll() .antMatchers("/user/getuser/**").permitAll() .antMatchers("/user/role/create").permitAll() .antMatchers("/department/create").permitAll() .antMatchers("/department/update/**").permitAll() .antMatchers("/department/delete/**").permitAll() .antMatchers("/speciality/create").permitAll() .antMatchers("/speciality/update/**").permitAll() .antMatchers("/speciality/delete/**").permitAll() .antMatchers("/pharmacy/create").permitAll() .antMatchers("/pharmacy/update/**").permitAll() .antMatchers("/pharmacy/delete/**").permitAll() .anyRequest().authenticated(); // Custom JWT based security filter JwtAuthorizationTokenFilter authenticationTokenFilter = new JwtAuthorizationTokenFilter(userDetailsService(), jwtTokenUtil, tokenHeader); httpSecurity .addFilterBefore(authenticationTokenFilter, UsernamePasswordAuthenticationFilter.class); // disable page caching httpSecurity .headers() .frameOptions().sameOrigin() // required to set for H2 else H2 Console will be blank. .cacheControl(); } @Override public void configure(WebSecurity web) throws Exception { // AuthenticationTokenFilter will ignore the below paths web .ignoring() .antMatchers( HttpMethod.POST, authenticationPath ) // allow anonymous resource requests .and() .ignoring() .antMatchers( HttpMethod.GET, "/", "/*.html", "/*.jsp", "/favicon.ico", "/**/*.html", "/**/*.css", "/**/*.js" ) // Un-secure H2 Database (for testing purposes, H2 console shouldn't be unprotected in production) .and() .ignoring() .antMatchers("/h2-console/**/**"); } } |
Commits for emr_demo/web_emr - Copy/src/main/java/com/bestray/config/WebSecurityConfig.java
Revision | Author | Commited | Message |
---|---|---|---|
2 | girijabapi | Thu 11 Oct, 2018 04:44:08 +0000 | initial commit |