
# Guarded screens

With this package, `@PreAuthorize` on a screen of [spring_boot_starter_flutter_navigation](../spring-boot-starter-flutter-navigation/index.md) (which it brings) takes the expressions of method security. Nobody logged in goes to `spring.flutter.navigation.login`, and back to the screen after the login, however it happens. A logged-in user without the right gets the error screen.

## Why

Which screens a user may open is one rule, wherever the navigation comes from: a tap, the browser's address bar, a deep link. The `routeSecurity` bean checks it for every navigation, with the application's `MethodSecurityExpressionHandler`, so the role hierarchy and custom expressions apply as on beans.

## Example

```dart
@ScreenRoute('/admin', title: 'Admin')
@PreAuthorize("hasRole('ADMIN')")
class AdminPage extends StatelessWidget { ... }

@ScreenRoute('/login', title: 'Log in')
class LoginPage extends StatelessWidget {
  const LoginPage({super.key});

  // After the login, the navigation returns to the guarded screen by itself.
  @override
  Widget build(BuildContext context) => const LoginForm();
}
```

```yaml
spring:
  flutter:
    navigation:
      login: /login
```

## The rules

- Nobody logged in goes to `spring.flutter.navigation.login`, and after the next login (a form, a provider's button, `restore()`) back to the address that was asked for.
- Without `login`, such a navigation shows the error screen.
- A logged-in user without the right gets the error screen.
- After a logout, the screen on top is opened again through the guards.
- In Dart configuration: `.filter(RouteSecurity.preAuthorize("hasRole('ADMIN')"))` on the routes of a `nest`.
- The guard is the `routeSecurity` bean, a `RouteAuthorizationFilter`. Without one (the navigation starter alone), a guarded route stops the start rather than open the route to everyone.

A guard hides screens on the device; the server still checks every request.

## See also

- [Filters, guards and errors](../spring-boot-starter-flutter-navigation/filters-and-guards.md)
- [Widgets that follow the user](principal-widgets.md)
- [spring_security_core](../spring-security-core/index.md)
