Add dependency with Spring boot :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
OR
Add dependency without Spring boot :
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
Security config 1 : Using properties
Add the basic properties in app.props
security.user.name=user1
security.user.password=pwd1
security.basic.authorize-mode=authenticated
security.basic.path=/**
It will provide the popup for authentication.
You can use configured credentials to access application.
Security config 2 : Without properties
Define a Security adapter class :
@EnableWebSecurity
public class MySecurityConfig extends WebSecurityConfigurerAdaptor {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user1")
.password("pwd1")
.roles("USER");
}
}
Define the Security adapter class with main application class :
@SpringBootApplication
public class TrainingApplication {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(
new Class[] { TrainingApplication.class, MySecurityConfig.class}, args);
}
}
It auto-creates a form with login and password.
For incorrect credentials, it provides default error messages.
No comments:
Post a Comment
Note: only a member of this blog may post a comment.