JAVA-13856 Create new security-modules (#12622)
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
package com.baeldung.springsecurity;
|
||||
|
||||
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
|
||||
|
||||
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
|
||||
|
||||
public SecurityWebApplicationInitializer() {
|
||||
super(SpringSecurityConfig.class);
|
||||
}
|
||||
}
|
||||
+46
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.springsecurity;
|
||||
|
||||
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;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
@Override
|
||||
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth
|
||||
.inMemoryAuthentication()
|
||||
.withUser("user1")
|
||||
.password("user1Pass")
|
||||
.roles("USER")
|
||||
.and()
|
||||
.withUser("admin")
|
||||
.password("adminPass")
|
||||
.roles("ADMIN");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.csrf()
|
||||
.disable()
|
||||
.authorizeRequests()
|
||||
.antMatchers("/auth/login*")
|
||||
.anonymous()
|
||||
.antMatchers("/home/admin*")
|
||||
.hasRole("ADMIN")
|
||||
.anyRequest()
|
||||
.authenticated()
|
||||
.and()
|
||||
.formLogin()
|
||||
.loginPage("/auth/login")
|
||||
.defaultSuccessUrl("/home", true)
|
||||
.failureUrl("/auth/login?error=true")
|
||||
.and()
|
||||
.logout()
|
||||
.logoutSuccessUrl("/auth/login");
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.springsecurity.controller;
|
||||
|
||||
import javax.mvc.annotation.Controller;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
|
||||
@Path("/home")
|
||||
@Controller
|
||||
public class HomeController {
|
||||
|
||||
@GET
|
||||
public String home() {
|
||||
return "home.jsp";
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/user")
|
||||
public String admin() {
|
||||
return "user.jsp";
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("/admin")
|
||||
public String user() {
|
||||
return "admin.jsp";
|
||||
}
|
||||
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.springsecurity.controller;
|
||||
|
||||
import javax.mvc.annotation.Controller;
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
|
||||
@Path("/auth/login")
|
||||
@Controller
|
||||
public class LoginController {
|
||||
|
||||
@GET
|
||||
public String login() {
|
||||
return "login.jsp";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user